Disable Alt+Enter for TreeViewer in Eclipse
mViewer.getTree().开发者_运维知识库addListener(SWT.KeyDown, new Listener() {
@Override
public void handleEvent(Event e) {
if(e.keyCode == SWT.CR && e.stateMask == SWT.ALT) {
e.doit = false;
}
}
});
Eclipse workbench filters some of the events before delegating the events to the listeners in the components. Is there a way i can disable the alt+enter to not execute the show properties in eclipse on one of the treeviewers?
Best Regards, Keshav
You can override a specific global command for a specific Viewer is many different ways:
- Add a listener to filter out the key sequence - though not always possible.
- Add a
Display
filter to do the same - the filter can be added/removed at the focus in/out on the control of the Viewer. - Add a new context to the application and override the needed key bindings for the new context - the context is activated/deactivated at the focus in/out on the control of the Viewer
I prefer this last solution as I can override specific key bindings for the Viewer from any plug-in using the normal binding extension point...
Try the following code:
mViewer.getTree().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR && e.stateMask == SWT.ALT) {
// your code
e.doit = false;
}
}
});
And the imports you need:
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
You will see all the key/down events - even if the key is a state key. So the first event is for the Alt
key down...
The sequence should be:
- KeyDown: stateMask=0 and keyCode=65536
- KeyDown: stateMask=65536 and keyCode='\r'
- KeyUp: stateMask=65536 and keyCode='\r'
- KeyUp: stateMask=0 and keyCode=65536
精彩评论