Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyTypedEvent KeyEvent's KeyCode is always 0?

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.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            keyTypedEvent(evt);
        }
    });

and keyTypedEvent is defined as such:

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

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

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

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.

like image 415
Rhs Avatar asked Feb 05 '13 19:02

Rhs


People also ask

What is keyCode in Java?

Description. The variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT. When checking for these keys, it can be useful to first check if the key is coded.

What does VK mean in java?

It's a shorthand for "Virtual Key": KeyEvent: Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").

What is a KeyEvent?

An event which indicates that a keystroke occurred in a component. This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed.


2 Answers

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.

like image 82
Russell Zahniser Avatar answered Nov 16 '22 00:11

Russell Zahniser


  • 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

like image 32
mKorbel Avatar answered Nov 15 '22 23:11

mKorbel