我正在我的小学校项目中研究暂停键,但由于某种原因它无法工作。使用此代码:
public void keyTyped(KeyEvent me) { //ESCAPE PLS WORK ...
code = me.getKeyCode();
System.out.println(code);
}
出于某种原因,“代码”始终保持为零。我尝试将其放在不同的空隙中(按下/释放等),但它仍然不起作用。可能是什么原因?
最佳答案
这就是javadoc说的是getKeyCode()
Returns: the integer code for an actual key on the keyboard. (For
KEY_TYPED
events, the keyCode isVK_UNDEFINED
.)
并且VK_UNDEFINED
的值为零。
javadoc还说:
public static final int KEY_TYPED
The "key typed" event. This event is generated when a character is entered. In the simplest case, it is produced by a single key press. Often, however, characters are produced by series of key presses, and the mapping from key pressed events to key typed events may be many-to-one or many-to-many.
所以也许您正在寻找错误的关键事件类型。也许应该查看 KEY_PRESSED
或 KEY_RELEASED
事件,而不是 KEY_TYPED
事件。
关于java - KeyEvent.getKeyCode() 始终返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814123/