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();
}