Main Tutorials

SWT – How to capture keyboard event

In SWT, keyboard event is represent by KeyEvent class. We can use KeyListener to receive and process a KeyEvent.

The KeyEvent class has three member fields to provide information about the key that generated by event.

1) character – Display a char value of the pressed key.
2) stateMask – Check whether any other keys currently pressed. (common use to capture CTRL, ALT and SHIFT keys).
3) keyCode – Display a digit value of the pressed key.

We can attach a KeyListener() or KeyAdapter() to a widget control to keep track the keyboard event.


"widget control".addKeyListener(new KeyAdapter()
{	
		public void keyPressed(KeyEvent e)
		{
		}
});

Check whether ALT key is currently pressed


if ((e.stateMask & SWT.ALT) != 0) 

Check whether BACKSPACE key is pressed


if(e.keyCode == SWT.BS)

P.S Please Check SWT class for other keyboard events.

Check whether any character is pressed


if(e.keyCode >=97 && e.keyCode <=122)

Check whether any digit is pressed


if(e.keyCode >=48 && e.keyCode <=57)

Here is the full source how to demonstrate how to capture the keyboard event.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class SWTKeyEvent
{
	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell(display);
		shell.setText("SWT KeyEvent Example");
	 
		shell.setLayout(new FillLayout());

		Button button = new Button(shell, SWT.CENTER);
		
		button.setText("Type Something");
		
		button.addKeyListener(new KeyAdapter()
		{	
			public void keyPressed(KeyEvent e)
			{
				String string = "";
				
				//check click together?
				if ((e.stateMask & SWT.ALT) != 0) string += "ALT - keyCode = " + e.keyCode;
				if ((e.stateMask & SWT.CTRL) != 0) string += "CTRL - keyCode = " + e.keyCode;
				if ((e.stateMask & SWT.SHIFT) != 0) string += "SHIFT - keyCode = " + e.keyCode;
				
				if(e.keyCode == SWT.BS)
				{
					string += "BACKSPACE - keyCode = " + e.keyCode;
				}
				
				if(e.keyCode == SWT.ESC)
				{
					string += "ESCAPE - keyCode = " + e.keyCode;
				}
				
				//check characters 
				if(e.keyCode >=97 && e.keyCode <=122)
				{
					string += " " + e.character + " - keyCode = " + e.keyCode;
				}

				//check digit
				if(e.keyCode >=48 && e.keyCode <=57)
				{
					string += " " + e.character + " - keyCode = " + e.keyCode;
				}
				
				if(!string.equals(""))
					System.out.println (string);
			}
		});
		
		shell.open();
	 
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

}

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Miroslav
12 years ago

Hello!

How to use this code in VBA?

I want to listen keypress in Excel cell

Thanks!

An Van
5 years ago

I encounter an issues when create an swt wizard page,
I created a List on the wizard page and I can’t interact with this list
When I’m try import the mylyn + egit plugins this issue resolved However
I want use wizard page without mylyn plugin, How can I do that.
Could you guys please help to look at this issue? Appreciate.

* This issuse just occur on eclipse for MAC OS

Naruto
7 years ago

Hello, I have a problem. I’m unable to listen key event (CTRL + S or CTRL + W).
Please help me, thank you so much.

DC MonsterSvk
9 years ago

I have a question… I’m a new user of eclipse and I just want to use some keylistener this looks nice but I don’t know how to use it where can I enter this code to worc because. I made a new class (public void main (string[] args) { but when I enter there the code it doesnt work I know there cant be 2x the public static void main string … so I deleted all and just let there the project name and class name and pasted it isnt work I tryed too thiuslike in swt but isn’t work too so can anyone help me ?:)

Dunxton
11 years ago

But how do I check for a shortcut something like ‘Ctrl+Alt+I’ which uses both Ctrl and Alt ?

David Churchill
10 years ago
Reply to  Dunxton

The ‘stateMask’ variable is a bitset – each bit of the integer represents whether a modifier, such as ALT or CTRL, was pressed at the time the event was generated.
To check if ALT is pressed:

if((e.stateMask & SWT.ALT ) != 0)

To check if CTRL is pressed:

if((e.stateMask & SWT.CTRL) != 0)

To check if ‘I’ is pressed you have to check the ‘keyCode’ variable which maps the key that pressed to generate the event to an ASCII value of the character. The ASCII value of ‘I’ is 73 so to check if I is pressed:

if(e.keyCode == 73)

However, Java has a shortcut for specifying character literals which will make the code cleaner:

if(e.keyCode == ‘I’)

Put it all together:

if( (e.stateMask & SWT.ALT ) != 0 &&
(e.stateMask & SWT.CTRL) != 0 &&
(e.keyCode == ‘I’)
{
// … CTRL+ALT+I was pressed
}