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
KeyTypedEvent KeyEvent's KeyCode is always 0?
提问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 keyTypedEvent
is defined as such:
并被keyTypedEvent
定义为:
private void keyTypedEvent(java.awt.event.KeyEvent evt)
{
System.out.println(evt);
appendDisplay(String.valueOf(evt.getKeyChar()));
}
I added a println
to the evt
to 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
我添加了一个println
以evt
查看会发生什么并验证我的密钥侦听器是否正常工作。当我构建并运行我的应用程序时,我意识到输出似乎总是有一个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
all suggestion about
KeyListener
forJButton
are wrong, meaningButton1.addKeyListener(new java.awt.event.KeyAdapter() {
these events are implemented and correctly in
JButtons API
, useSwingAction
or addActionListener
for listeningMouse and Key Event
from/toJButton
basically everything is described in Oracle tutorial about How to Use Buttons, Check Boxes, and Radio Buttons
所有关于
KeyListener
for 的建议JButton
都是错误的,意思是Button1.addKeyListener(new java.awt.event.KeyAdapter() {
这些事件在
JButtons API
、 使用SwingAction
或添加中正确实现并ActionListener
用于收听Mouse and Key Event
/收听JButton
基本上所有内容都在关于如何使用按钮、复选框和单选按钮的Oracle 教程中进行了描述
回答by 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.
这在很大程度上取决于已按下的键。也许你需要KeyListener
与keyPressed
方法重写,因为keyTyped
在非打印字符不会被触发。
Look at the difference between keyTyped
and keyPressed
here:
KeyListener, keyPressed versus keyTyped
看看这里keyTyped
和keyPressed
这里的区别:
KeyListener, keyPressed vs keyTyped