使用swt处理按键

时间:2010-10-06 18:00:08

标签: java user-interface swt keylistener draw2d


按下 CTRL-C 时,我想要 someStuff()。我写了一些代码,但它“不够好”。我该怎么办?)

    //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();
            } 
        }
    });

感谢。

2 个答案:

答案 0 :(得分:7)

试试这个:

getShape().addListener(SWT.KeyUp, new Listener() {
    public void handleEvent(Event event) {
        if (event.stateMask == SWT.CTRL && event.keyCode == 'c') {
            someStuff();
        }
    }});

答案 1 :(得分:0)

尝试使用以下条件检查CTRL-C:

public void keyReleased(KeyEvent event) {
  if (event.getKeyCode() == KeyEvent.VK_C && event.getModifiers() == InputEvent.CTRL_MASK)
    someStuff();
}