Associating an Action with a key binding [message #524827] |
Fri, 02 April 2010 15:14  |
Eclipse User |
|
|
|
I have a plugin that displays a representation of a custom file system in a tree. There is a custom menu that appears when you right mouse click on a node in the tree. I have a bunch of actions associated with those entries in the menu(deleteAction, cutAction, etc).
Everything works fine if I use the mouse. I can right click, select a node, and then select Delete from the menu and the Action is fired. The issue is I can't seem to figure out any way to associate the "DEL" key with the same operation.
I found this link: http://www.vogella.de/articles/EclipseCommands/article.html# keybinding
but it appears that does something with a "Command" as opposed to an "Action". So, is there any example out there that shows how to associate a key or key sequence with an Action or do I have to try and use "Command"s instead? I don't have any CommandID to refer to in my plugin.xml.
Thanks,
Andy
|
|
|
|
|
Keybinding And Command Step By Step Instructions [message #533970 is a reply to message #524827] |
Mon, 17 May 2010 11:12  |
Eclipse User |
|
|
|
Thought I would post this to help anyone else out trying to get a keybinding to work with a command. In this example I want to override the DEL key in Eclipse for my window.
1) Go to your plugin.xml => extensions tab => add org.eclipse.ui.commands
2) Right mouse on org.eclipse.ui.commands => new => command
3) Enter an id such as com.mycompany.commands.delete.
4) Enter a name, such as "Delete"
5) Click the defaultHandler link. On the resulting screen assign a package, have the class extend org.eclipse.core.commands.AbstractHandler, and enter a name for the class. In this case it is called com.mycompany.handlers.DeleteHandler. Leave the class as it stands for now.
6) Back in plugin.xml on the extensions tab, right mouse on org.eclipse.ui.commands => new => context.
7) Enter an id for the context, such as com.mycompany.contexts.forkeybinding
Enter a name, such as ForKeyBinding
9) Important! Enter the parentId as org.eclipse.ui.contexts.window. Eclipse leaves this field empty by default and the command will fail if this isn't added.
10) Still on the plugin.xml / extensions tab, press Add. Add org.eclipse.ui.bindings.
11) Right mouse on org.eclipse.ui.bindings => key. In the resulting screen enter the following:
sequence: DEL (If you want to override a different key, right mouse over the text "sequence*". A dialog will appear that will explain key combinations)
schemeId: org.eclipse.ui.defaultAcceleratorConfiguration
contextId: com.mycompany.contexts.forkeybinding (The id of the context we just created)
commandId: com.mycompany.commands.delete (The id of the command we created earlier)
12) At this point if you run your plugin the DEL key will now fire off the execute method in the command handler. Add whatever logic is necessary that you want achieved in the command.
13) If you have a ViewPart that needs to be updated as a result of the command, the way to get a hold of the ViewPart from within your command is as follows:
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart viewer = HandlerUtil.getActivePart(event);
}
Then you can fire an event into the viewer and update your display.
|
|
|
Powered by
FUDForum. Page generated in 0.24105 seconds