29.7.08

SWT - Detecting Ctrl +C key combination

One of my colleague had a requirement to detect the Ctrl +C key combination in a Eclipse View .

I wrote this sample snippet which works

import org.eclipse.swt.*;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.*;

public class CtrlCTest{

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.stateMask == SWT.CTRL && e.keyCode == 99) {
System.out.println("CTRL +C");
}
}
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

0 opinions: