KeyTypedEvent KeyEvent's KeyCode is always 0?

后端 未结 3 453
-上瘾入骨i
-上瘾入骨i 2020-12-06 10:35

I have a Java Swing application in the NetBeans IDE.

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

    jButton1.addKeyList         


        
相关标签:
3条回答
  • 2020-12-06 10:43
    • all suggestion about KeyListener for JButton are wrong, meaning Button1.addKeyListener(new java.awt.event.KeyAdapter() {

    • these events are implemented and correctly in JButtons API, use SwingAction or add ActionListener for listening Mouse and Key Event from/to JButton

    • basically everything is described in Oracle tutorial about How to Use Buttons, Check Boxes, and Radio Buttons

    0 讨论(0)
  • 2020-12-06 10:43

    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

    0 讨论(0)
  • 2020-12-06 10:50

    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.

    0 讨论(0)
提交回复
热议问题