I have a Java Swing application in NetBeans IDE.
I made a form and bound the 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 println to evt to find out what is happening and check if my keylistener is working. When I create and run my application, I realized that the output is always keycode = 0
To test this, I changed my println to evt.getKeyCode() and it always returns 0.
I could have completely misinterpreted what KeyCode does, but I thought it would follow the values ββin the Oracle documentation here:
http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_ESCAPE
For example, VK_ESCAPE has a value of 27.
java swing keylistener keyevent
Rhs
source share