Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic