Add a Key Listener to TitleAreaDialog

2019-09-16 18:51发布

问题:

I need to add a key listener to my TitelAreaDialog is there any solution to do this ?

回答1:

You can add a Listener to the Display by using:

Listener listener = new Listener() {
    public void handleEvent(Event event) {
        System.out.println(event.character);
    }
}
getShell().getDisplay().addFilter(SWT.KeyDown, listener);

This will output all pressed keys without consuming the events, i.e. the underlying widgets will still register the events.


Remember to remove it again in the close() method of the Dialog:

@Override
public boolean close()
{
    getShell().getDisplay().removeFilter(SWT.KeyDown, listener);
    super.close();
}