Reputation: 1409
Currently I am trying to integrate MKYong's KeyListener example into an Eclipse RCP app.
I'm getting some weird behavior from a Button
I am registering the KeyListener
to, it only starts listening to keyboard input after the button was clicked the first time.
I would like it to listen to all keyboard events occurred after it was created. To me it is irrelevant what sort of SWT Control
I would have to use.
Any thoughts are appreciated.
Upvotes: 0
Views: 85
Reputation: 516
You've added KeyListener to Button. Button is notified of keyEvent when it got focus. If you want to listen for keyboard events in your whole window then add KeyListener to your shell:
shell.addKeyListener(new KeyAdapter()...
If you want global KeyListener add display filter:
Listener listener = new Listener() {
@Override
public void handleEvent(Event e) {
if(e.type == SWT.KeyDown||e.type == SWT.KeyUp) {
System.out.println("" + e.keyCode);
}
}
};
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.KeyUp, listener);
Upvotes: 1
Reputation: 111217
A control only gets key events when it has the keyboard focus.
If you want to get all key events you can use the addFilter
method of Display
Display.getDefault().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(Event event)
{
// Handle key down event
}
});
Upvotes: 2