Handle key presses using swt
Asked Answered
J

2

7


I'm want do someStuff() when CTRL-C pressed. I had wrote some code, but it "not enough good". What can I do with it?)

    //org.eclipse.draw2d.Shape getShape(){....}

    getShape().addKeyListener(new KeyListener(){

        @Override
        public void keyPressed(KeyEvent ke) {
        }

        @Override
        public void keyReleased(KeyEvent ke) {
            /*if CTRL-C pressed*/
            if((ke.getState() & KeyEvent.CONTROL) != 0 && 'c' == ('a' + ke.character - 1)){
                someStuff();
            } 
        }
    });

Thanks.

Jowl answered 6/10, 2010 at 18:0 Comment(0)
B
7

Try this:

getShape().addListener(SWT.KeyUp, new Listener() {
    public void handleEvent(Event event) {
        if (event.stateMask == SWT.CTRL && event.keyCode == 'c') {
            someStuff();
        }
    }});
Blamed answered 7/10, 2010 at 5:51 Comment(0)
B
0

Try using the following condition to check for CTRL-C (EDIT: AWT/Swing):

public void keyReleased(KeyEvent event) {
  if (event.getKeyCode() == KeyEvent.VK_C && event.getModifiers() == InputEvent.CTRL_MASK)
    someStuff();
}
Bacteriostat answered 6/10, 2010 at 19:15 Comment(2)
org.eclipse.draw2d.KeyEvent doesn't contain any VK_C and so on. I'm sorry if I formulate question in some not obvious form.Jowl
Aah, my mistake. I don't know how to help you then =/.Bacteriostat

© 2022 - 2024 — McMap. All rights reserved.