SWT: define a global keylistener for your display

Source: Internet
Author: User

Display has such a method:

Public void addfilter (INT eventtype, listener)

That is, add a global listener to the display. For example, the shortcut key Ctrl + A is defined for the program above.

The display filter is very powerful. You can monitor the CTRL + A event no matter which control the current focus is on.

If there are other controls on the interface that listen to this shortcut key, the listener in this display will be executed first than the listener of other controls.

The following code comes from

Http://www.vogella.de/articles/SWT/article.html
Define a global keylistener for your display
You can define a global keylistener on your display to listen globally to all key events as long as your application has focus. The following demonstrates this.

Package de. vogella. SWT. listener;

Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. event;
Import org. Eclipse. SWT. Widgets. listener;
Import org. Eclipse. SWT. Widgets. shell;

Public class globallistener {

Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. open ();
Display. addfilter (SWT. keydown, new listener (){
@ Override
Public void handleevent (event ){
Char c = event. character;
System. Out. println (C );
}
});

While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.