我在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;
}
});