SWT: Detecting which key was pressed during a MouseClick by Lars Vogel

Sometimes you want to detect the metakey which was pressed during a mouse click on SWT widget. This information is available in the SelectionEvent via the stateMask property.

The following code shows an example for this.

[code source=”java”]
Button button = new Button(shell, SWT.READ_ONLY);
button.setText("Press me");
button.setSize(200, 200);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if ((e.stateMask & SWT.CTRL) != 0) {
System.out.println("SWT.CTRL");
}
}
});

[/code]

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences,

He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.

This entry was posted in Eclipse, Lars Vogel and tagged , . Bookmark the permalink.