Register keystroke for EditorTextField

My plugin hosts an EditorTextField where the user can edit and execute an ad hoc script in the support custom language.  I'd like to register CTRL+ENTER/CMD+ENTER as a keystroke that can be used to execute the script.  Currently there's an Execute button in a toolbar with a registered mnemonic that requires the ALT/OPT modifier.  I've tried using the standard Swing method for registering a keyboard action with the EditorTextField component, but it doesn't seem to work here.  That's not entirely surprising since that very rich editor component is reacting to the selected/configured keymap.

IDEA's own "Evaluate Expression" debugger window does allow this, though, so I'm really wondering how that's accomplished.

Thanks in advance for any guidance!

Comment actions Permalink

Hi Scott,

I think AnAction#registerCustomShortcutSet(ShortcutSet, JComponent) is the call you want. I do this in my REPL editor, which sounds similar to what you're talking about.

BTW consider maintaining the execute action as a public action that users can bind a keystroke to. Initially I had a private action which was created and bound explicitly to that component, but that meant that users couldn't customise the keybinding. This was a problem for a couple of users who used Windows at work and Mac at home - they wanted to bind the action to Ctrl-Enter on both. You can use something like:

(find the action you want to bind to the component, probably using ActionManager#getAction)

ShortcutSet shortcut = action.getShortcutSet()

if (shortcut != null)

  action.registerCustomShortcutSet(shortcut, component)


If you have an editor, you can use getComponent() to get the component to use.

Comment actions Permalink

Thanks so much, Colin! This is exactly what I needed.  And great idea on registering it as a configurable action.


Please sign in to leave a comment.