Catching Multiple Hotkeys in Editor?
I'm writing a plugin for IntelliJ to handle a custom language in a specific file type and it includes about 20 unicode characters which need to be bound to hotkeys for usability. I have two to three problems getting these wired up:
- I can't find any code on how to utilize EditorActionHandler or EditorAction to bind a key combination and insert a character at the caret when the file has focus in the editor (or a pair of characters around a selection if there is a selection.)
- I'm not sure if that's the right approach with so many hotkeys (I'd prefer to have them in an array or even an if/else-if block than to make 40+ separate classes for actions and action handlers - I'd also like to be able to adjust these dynamically based on the output from the lexer since the language itself may define new hotkeys for interaction.)
- Thus far I've only been able to catch typed keys in the editor, but not key combinations.
Any help would be appreciated, especially with code samples since I'm new to plugin development for IntelliJ and can't find any related code samples in the tutorials or a comprehensive API reference - just some short writeups and samples on the more generic seeming AnAction overrides to add menu items.
Please sign in to leave a comment.
One possible source for examples is a source code for IDEA Community Edition - you can get it on Github. E.g. a quite simple example of an action, that modifies code in editor is HungryBackspaceAction. Another example of an editor action is given in IntelliJ Platform SDK DevGuide here.
An action can be assigned multiple shortcuts. To be able to do different things on different shortcuts in the same action, you'll need to dispatch on InputEvent contents in your action code. EditorAction doesn't expose this data, but if you extend from AnAction directly, you are passed AnActionEvent instance, which contains a reference to InputEvent. You can also assign shortcuts to actions programmatically, using KeymapManager (KeymapManager.getInstance().getActiveKeyMap().addShortcut(...)).
Not sure I understand the problem with handling key combinations. Is it about assigning shortcuts like Ctrl+O? That is done in the same way - in your plugin XML, just set "control O" as a keystroke for an action.
This seems to be getting closer, but I'm still at a loss as to how to interact with addShortcut, Shortcut, KeyStroke, and actually register the event. Is there any actual reference/documentation on this aside from the tutorials which don't exactly match the specific task?
In my AnAction-overriding constructor I have the incomplete code:
While in the actionPerformed call I have:
But am also at a lost as to how I get the actual key pressed in the actionPerformed function (though the modifiers appear to all be included in the inputEvent.)
InputEvent and KeyStroke are AWT/Swing classes, I don't think we have any documentation on using them, you can read corresponding javadocs in JDK, or, probably, in some other AWT/Swing resources.
If InputEvent represents a keyboard event, you can cast it to KeyEvent (after instanceof check), and get more information from it.
If you think something is missing in IntelliJ Platform classes' javadocs, you can mention it here, we'll try to add missing information.
I think this got me a lot closer but am still missing something, my class currently looks like:
and the registration looks like:
Is this being registered incorrectly? I'm not seeing any message boxes when I run the plugin.
The actions are initialized in a background thread, and showing message dialog is only possible from event dispatch thread - you should have exception in log about that.
Also, you're using wrong actionId when you're calling addShortcut - it doesn't match action id you're using when registering your action.
This seems to be the information I needed, thanks.
For anyone else who comes across this and wants a code sample, this works up through keybinding and outputting the result to the log:
I spoke a bit soon, it seems no matter how I try to implement it I'm getting the error:
2018-05-31 11:45:08,984 [ 12688] ERROR - plication.impl.ApplicationImpl - Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable: Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
Is there a special way to go about this? I've tried implementing the HotkeyHandler class such that it overrides a variety of different EditorActions and EditorActionHandlers called by the HotkeyAction which inherits from AnAction.
What's the appropriate way to make changes in the editor from within a class inheriting from AnAction?
I suggest you go through IntelliJ Platform SDK DevGuide again - it has an answer to this question (and possibly some future questions).
To modify document you should be holding a write lock (using Application.runWriteAction()) and executing a command (CommandProcessor.executeCommand). Or you can combine both, like in the example I've mentioned previously, by using WriteCommandAction class.
Thanks, I think I have it after reading that, just issues moving carets and selections around now but I should be able to figure that out.