对于单个按键,控制进入多次

时间:2013-05-10 06:39:04

标签: java swing awt keyevent keyboardfocusmanager

我在Java Swing中有一段禁用击键的代码。但是当我按一次键时,控件多次输入代码。

我附上我的代码:

KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(new KeyEventDispatcher() {
                public boolean dispatchKeyEvent(KeyEvent ke) {
                    if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() instanceof JTextArea) {
                        if (ke.getID() == KeyEvent.KEY_PRESSED) {
                            int key = ke.getKeyCode();
                            if (key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN) {
                                arrowKey = true;
                                ke.consume();
                            }
                        }
                        if (ke.getID() == KeyEvent.KEY_TYPED && arrowKey) ke.consume();
                        if (ke.getID() == KeyEvent.KEY_RELEASED) arrowKey = false;
                    }
                    return false;
                }
            });

0 个答案:

没有答案