简体   繁体   English

Java中键盘事件的怪异行为

[英]Weird behavior of keyboard events in Java

When I press "Delete" button on the keyboard, program gets three events - KEY_PRESSED , KEY_TYPED , and KEY_RELEASED . 当我按下键盘上的"Delete"按钮时,程序将获得三个事件KEY_PRESSEDKEY_TYPEDKEY_RELEASED The problem is, in KEY_PRESSED and KEY_RELEASED , parameter "keyCode" is set, but in the KEY_TYPED it is not (in fact, there no meaningful info in that event). 问题是,在KEY_PRESSEDKEY_RELEASED ,设置了参数"keyCode" ,但在KEY_TYPED中未设置(实际上,在该事件中没有有意义的信息)。 With F5 key, it is even funnier - KEY_PRESSED and KEY_RELEASED are registered, but KEY_TYPED never occurs. 使用F5键,它甚至更有趣-已注册KEY_PRESSEDKEY_RELEASED ,但从不发生KEY_TYPED

The listener was added via Toolkit.getDefaultToolkit().addAWTEventListener() . 侦听器是通过Toolkit.getDefaultToolkit().addAWTEventListener() Using JDK 6.26. 使用JDK 6.26。

What could be my problem? 我可能是什么问题?

EDIT: Here are the events that happen when Delete key is pressed: 编辑:这是按下Delete键时发生的事件:

 java.awt.event.KeyEvent[KEY_PRESSED,keyCode=127,keyText=Delete,keyChar=Delete,keyLocation=KEY_LOCATION_STANDARD,rawCode=119,primaryLevelUnicode=127,scancode=0] on javax.swing.JButton[,0,0,61x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@50f38cf0,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@6ae2d0b2,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=SVG,defaultCapable=false]
 java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Delete,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=127,scancode=0] on javax.swing.JButton[,0,0,61x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@50f38cf0,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@6ae2d0b2,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=SVG,defaultCapable=false]
 java.awt.event.KeyEvent[KEY_RELEASED,keyCode=127,keyText=Delete,keyChar=Delete,keyLocation=KEY_LOCATION_STANDARD,rawCode=119,primaryLevelUnicode=127,scancode=0] on javax.swing.JButton[,0,0,61x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@50f38cf0,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@6ae2d0b2,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=SVG,defaultCapable=false]
  • better would be implements KeyBindings 更好的是实现KeyBindings

  • part of keyboard are reserved Keys for built-in JComponent funcionality, more informations from @camickrs UIManager Defaults 键盘的一部分保留为内置JComponent功能的键,有关更多信息,请参见@camickrs UIManager默认值

  • depends of reason(s) why you needed implents KeyListener, because for there are lots of another Listeners for various JComponent, that should be filtering or register text changes inside 取决于您需要为什么使用KeyListener的原因,因为对于各种JComponent,还有许多其他的Listener,它们应该在内部过滤或注册文本更改

  • some of JComponent Models generated Events from Mouse and Keyboard input 一些JComponent模型通过鼠标和键盘输入生成事件

Key typed events ALWAYS generate '0' as the key code. 按键类型的事件始终会生成“ 0”作为按键代码。 Look up the method getKeyChar() instead, or (as has been suggested) listen for keyReleased() instead. 查找方法getKeyChar()代替,或者(如建议的那样)监听keyReleased()。 getKeyChar(): http://goo.gl/ajH03 getKeyChar(): http//goo.gl/ajH03

From the JavaDoc 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. 来自JavaDoc 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. 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.

You are trying to get the F5 key which is probably not registered as a character being entered. 您正在尝试获取可能未注册为输入字符的F5键。 By using the KEY_RELEASED you will consistently get the result you are looking for and the API is behaving as expected. 通过使用KEY_RELEASED,您将始终获得所需的结果,并且API的行为符合预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM