Search code examples
eclipse-pluginswteclipse-rcprcp

How to implement delete key binding in RCP 3.X


I need to implement a delete key binding, so that when i press delete key, a selected tree node has to be deleted. I have already implemented deleting in a buttonListener, but i need to implement the same with the DEL Key as well.

Plugin.xml

<extension
     point="org.eclipse.ui.bindings">
  <key
        commandId="org.eclipse.ui.edit.delete"
        contextId="org.eclipse.ui.contexts.dialogAndWindow"
        schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
        sequence="M1+DEL">  //just tried for CTRL+DEL key but i need only for DEL Key
  </key>
</extension>

Should i add commands? What should be in the attributes of commands? Where should i call this action in my code?


Solution

  • Most plugins just use a key listener on the tree for this. Something like:

    treeViewer.getTree().addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent event) {
          if (event.character == SWT.DEL && event.stateMask == 0) {
             // TODO handle delete
          }
        } 
    });
    

    Note that Eclipse already a binding for delete:

    <key
       commandId="org.eclipse.ui.edit.delete"
       sequence="DEL"
       schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
    

    so you may also be able to define a handler for the command org.eclipse.ui.edit.delete but you must make sure the handler is only active when your view/editor is active.