• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

KeyEvent.getKeyCode() returning 0

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
}
}
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic