Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyEvent.getKeyCode() returning 0  RSS feed

 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
I am trying to detect when the user pushes backspace or delete but when the key event issent to my listener the keycode is 0?
Am I doing something wrong?
Rob
public void keyTyped(KeyEvent event) {
Object component = event.getSource();
if ( component instanceof JTextField ) {
JTextField field = (JTextField)component;
System.out.println(event.getKeyCode());
System.out.println(event.getKeyChar());
if ((event.getKeyCode() == KeyEvent.VK_BACK_SPACE) ||
(event.getKeyCode() == KeyEvent.VK_DELETE)) {
//user deleted their input all components should be
//enabled
if (field.getText().length() == 0 ) {
Component c[] = getComponents();
for (int i = 0; i < c.length; i++) {
JComponent temp = (JComponent)c[i];
temp.setEnabled(true);
}
}
}
else {
Component c[] = getComponents();
for (int i = 0; i < c.length; i++) {
JComponent temp = (JComponent)c[i];
if (!temp.hasFocus()) {
temp.setEnabled(false);
}
}
}
}
}
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u have to call getKeyChar() method while checking u r conditioncal checks not getKeyCode().
getKeyCode() Returns the integer keyCode associated with the key in this event.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!