• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

keyboard events

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Crusading Chameleon likes the size of this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic