How to create a key binding in your Eclipse RCP application?
You need to create a command and its command handler and then create a key binding for it.
1. Define a command:
See an example below for a Save command:
I have a defaulthandler specified and I have categorized this command under the MyCategory category. This is useful when this command is shown in the Keys view in the Preferences dialog.
<extension point="org.eclipse.ui.commands">
<category description="My category for commands" id="MyCategory" name="My Category">
</category>
<command categoryid="MyCategory" description="Save the active screen" defaulthandler="my.package.MySaveHandler" id="save.command" name="Save">
</command>
</extension>
2. Create a command handler for the command:In the above step I defined the defaultHandler as my.package.MySaveHandler. This is the fully qualified class name for the handler class that implements IHandler. This is called when this command is executed.
3. Create a key binding for and bind it to the command:Shown below is the minimal key binding for the CTRL+S key sequence. It is bound to the save command through the commandId property. the schemeId is set to the default key binding scheme. This works for me because there arent any other key bindings for CTRL+S defined in the default scheme. If you are faced with a situation where the default scheme already has a binding for your sequence, then you can either define your key binding in another scheme (See the 'Defining your own key binding scheme' section below for more information.) or you can define your key binding's active context. ( See How to selectively enable or disable the key binding)
<extension point="org.eclipse.ui.bindings">
<key commandid="save.command" schemeid="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="M1+S">
</key>
</extension>
How to selectively enable or disable the key binding?This is the same as enabling or disabling the command associated with the key binding.
You do this by using the org.eclipse.ui.handlers extension point. In the example below I enable my handler for the command only if a specific part is active
<extension point="org.eclipse.ui.handlers">
<handler class="my.package.MySaveHandler" commandid="save.command">
<enabledwhen>
<with variable="activePartId">
<equals value="my.package.editors.MyEditor">
</equals>
</with>
</enabledwhen>
</handler>
</extension>
The handlers extension is also useful when you want to have multiple handlers for the same command. For e.g. if you want your save command to save the currently active editor, and you have different types of editors in your application each requiring a different saving mechanism, you can define three different handlers, bind them all to the Save command through the commandId property and just make sure only one of them is active based on which editor is active at the time the command is invoked. Actually, when it comes to editors you can get away with one handler because all editors that extend IEditorPart have a doSave method anyways which can be called by the common handler. You implement your differing save mechanisms by overriding the doSave method in your editor.
How to override an existing key binding?There are two ways to do this, one, by defining your key binding in your own scheme, or two, by defining the context in which this key binding is active.
1. Defining your own key binding scheme:Using the bindings extension point you can define your own scheme and then using the schemeId property of the key element you can define your key binding to belong to your schema.
See
this Vogel.de tutorial on how to do this.
2: Defining context for your key binding:there are three steps as shown below...
a. Define your context: You can do this using the org.eclipse.ui.contexts extension.
<extension point="org.eclipse.ui.contexts">
<context id="my.context" name="My Context" parentid="org.eclipse.ui.contexts.window">
</context>
</extension>
b. Set the key binding's context to the one you defined in a.:Each key binding has the contextId property that you can use for this..
<extension point="org.eclipse.ui.bindings">
<key commandid="save.command" contextid="my.context" schemeid="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="M1+S">
</key>
</extension>
c. Switch the context in your application
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
((IContextService) PlatformUI.getWorkbench()
.getService(IContextService.class))
.activateContext("my.context");
}
});
If you want to do this at the start a good place to do it is in the createInitialLayout method of the IPerspectiveFactory class.
Resources:http://www.vogella.de/articles/EclipseCommands/article.html#keybinding
http://sry-for-my-english.blogspot.com/2007/05/rcp-keybinding-issues.html