| Keybindings |
|
|
This article describes how to declare keybindings using the Ecipse framework.
You can define keybindings for any action. Eclipse provides a mechanism to do this, so that there are no conflicts between plugins. This is done using "commands". A command can be seen as an abstract description of a key binding. The command itself has no keybinding assigned to it. But given a certain context, e. g. a certain view window, you can declare, that in this context, the command should have a certain key binding. Let's consider an action "renameAction", which is used to rename some object, e. g. a file name. You can find this example in the plugin "elexis-externe-dokumente". Generally, you need to define a command for each action you want to assign keybindings to. You can use existing actions already defined by Eclipse, like e. g. "org.eclipse.ui.edit.rename", or you can define your own actions in the plugin editor. Each command has a unique id. The pre-defined action for renaming has the id "org.eclipse.ui.edit.rename". To use this command, you must first assign it to the action, and then register it an action handler for it. renameAction = new Action() { ... } ... renameAction.setActionDefinitionId(GlobalActions.RENAME_COMMAND); GlobalActions.registerActionHandler(this, renameAction); This should be all required to do for a pre-defined command. If you want to define your own key bindings, you require to define a context, a command and a binding. Let's consider an keybinding to export data to the clipboard. The key binding should be "CTRL-E". First, declare a new context in plugin.xml: <extension point="org.eclipse.ui.contexts"> Next, declare a command (including a category, if required): <extension point="org.eclipse.ui.commands"> <category id="org.iatrix.commands.category" name="%iatrix.commands.category"> </category> <command categoryId="org.iatrix.commands.category" id="org.iatrix.commands.export_clipboard" name="%iatrix.commands.export_clipboard"> </command> </extension> Then declare a binding <extension point="org.eclipse.ui.bindings"> Now, the new command is known, including the context where it should be valid, and the key binding to be used. In the view, where you want to use the command, you first need to declare the context: Declare the context id and the command id as constants: private static final String VIEW_CONTEXT_ID = "org.iatrix.view.context"; //$NON-NLS-1$ At the end of your createPartControl() method, call "activateContext()", which may defined as: /** Whenever the Iatrix view is active, the context "org.iatrix.view.context" will be active, and the key bindings for it will be valid. Then, assign the command to the action as in the previous example: exportToClipboardAction = new Action("Export (Zwischenablage)") {Further documentation: http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/wrkAdv_keyBindings.htm |
| weiter > |
|---|
