java KeyTypedEvent KeyEvent 的 KeyCode 总是 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14714888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:07:55  来源:igfitidea点击:

KeyTypedEvent KeyEvent's KeyCode is always 0?

javaswingkeylistenerkeyevent

提问by Rhs

I have a Java Swing application in the NetBeans IDE.

我在 NetBeans IDE 中有一个 Java Swing 应用程序。

I made a form and attached a KeyListener to my various controls as such:

我制作了一个表单并将一个 KeyListener 附加到我的各种控件上:

    jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            keyTypedEvent(evt);
        }
    });

and keyTypedEventis defined as such:

并被keyTypedEvent定义为:

private void keyTypedEvent(java.awt.event.KeyEvent evt) 
{                               
System.out.println(evt);
appendDisplay(String.valueOf(evt.getKeyChar()));
} 

I added a printlnto the evtto see what happens and to verify that my keylistener does work. When I build and run my application, I realized that the output always seems to have a keycode = 0

我添加了一个printlnevt查看会发生什么并验证我的密钥侦听器是否正常工作。当我构建并运行我的应用程序时,我意识到输出似乎总是有一个keycode = 0

To verify this, I had changed my println to be evt.getKeyCode()and it is always returning 0.

为了验证这一点,我已将 println 更改为 ,evt.getKeyCode()并且它始终返回 0。

I could be completely misinterpreting what KeyCode does, but I thought that it would coorespond with the values in Oracle's documentation here:

我可能完全误解了 KeyCode 的作用,但我认为它会与此处 Oracle 文档中的值一致:

http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_ESCAPE

http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_ESCAPE

For instance, VK_ESCAPE has a value of 27.

例如,VK_ESCAPE 的值为 27。

回答by Russell Zahniser

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().

keyTyped()事件仅用于产生字符输入的键。如果您想知道何时按下或释放任何键,则需要实现keyPressed()keyReleased()

From the KeyEventAPI:

来自KeyEventAPI:

"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.

“按键类型”事件是更高级别的,通常不依赖于平台或键盘布局。它们是在输入 Unicode 字符时生成的,是查找字符输入的首选方式......

对于按键按下和按键释放事件,getKeyCode 方法返回事件的 keyCode。对于键类型事件,getKeyCode 方法始终返回 VK_UNDEFINED。

回答by mKorbel

回答by mishadoff

It very depends on key that has been pressed. Probably you need KeyListenerwith keyPressedmethod override, because keyTypednot triggered on non-printable characters.

这在很大程度上取决于已按下的键。也许你需要KeyListenerkeyPressed方法重写,因为keyTyped在非打印字符不会被触发。

Look at the difference between keyTypedand keyPressedhere: KeyListener, keyPressed versus keyTyped

看看这里keyTypedkeyPressed这里的区别: KeyListener, keyPressed vs keyTyped