• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

keyboard events

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to output key the user presses. why won't this work? I don't think it is catching the key event.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of all, it won't compile. In your keyPressed method, you are calling getKeyText(), which requires an int as an argument. I suspect you meant to use getKeyChar().
Second, you need to add the KeyListener object to the component that is visible - that is, to the JTextField showkey, not to the container pane.
Try it. You should see both your keyPressed and keyTyped event handlers for the character keys, and just your keyPressed for the non-typeable keys.
One more suggestion - while you're learning, you may want to include in your printout the name of method that is showing the information, such as "System.out.print("in keyPressed " + x);" or "System.out.print("in keyTyped " + key);". Helps you to learn the differences between a keyPressed, keyTyped, and keyReleased.
Have fun!
 
mike smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, i'll try that. And yea I have been using the system output commands for learning and debugging purposes.
 
See where your hand is? Not there. It's next to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic