KeyTypedEvent KeyEvent's KeyCode is always 0?

那年仲夏 提交于 2019-11-27 23:34:30

The keyTyped() event is only used for keys that produce character input. If you want to know when any key is pressed or released, you need to implement keyPressed() or keyReleased().

From the KeyEvent API:

"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input....

For key pressed and key released events, the getKeyCode method returns the event's keyCode. For key typed events, the getKeyCode method always returns VK_UNDEFINED.

mishadoff

It very depends on key that has been pressed. Probably you need KeyListener with keyPressed method override, because keyTyped not triggered on non-printable characters.

Look at the difference between keyTyped and keyPressed here: KeyListener, keyPressed versus keyTyped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!