• 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

Recognizing Keys Pressed with a JPanel

 
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking of writing a class that extends {javax.swing.JPanel}, where a lot of things happen when I click on it, but I'd also like it to be able to recognize keys pressed. Can anyone point me to where I can find out how I can write code that will recognize keys pressed, like alphanumeric keys, special characters, the escape key, the four directional movement keys, etc.?
 
Marshal
Posts: 69814
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin, to the Batmobile Java┬« Tutorials. If you push ctrf‑F and key you find two links which are relevant, about key bindings and key listeners. I think you want the latter. There are several other links, but they are about cryptographic keys. You can find a list as long as your arm of the different keys: here.

Remember that a key is a low‑level event, and as a general rule you should use the highest‑level events you can.
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Robin, to the Batmobile Java┬« Tutorials. If you push ctrf‑F and key you find two links which are relevant, about key bindings and key listeners. I think you want the latter. There are several other links, but they are about cryptographic keys. You can find a list as long as your arm of the different keys: here.

Remember that a key is a low‑level event, and as a general rule you should use the highest‑level events you can.


Thanks for the input, Campbell.

Okay, I did some poking around and discovered interface {KeyListener} and method {addKeyListender()}, and they looked pretty promising, so I wrote "Kc.java" that's coded as follows:

This compiles just fine, and I can click on it and have it print out the location where I clicked it, but when I type the "A" key I was hoping it would print "Keyed character 'a' with code ...", but instead nothing happens. Anybody have any idea why? Do I have to set the focus to my {JPanel}? If so, how do I set the focus to my {JPanel}?
 
Campbell Ritchie
Marshal
Posts: 69814
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the button for code tags, don't write them by hand; I had to remove the "" to make them work.
I added SY_O.print(keyTyped); to your keyTyped() method, which is a good way to see whether it works. It didn't. It says in the tutorials link that you have to implement a setFocusable method, so I added setFocusable(true) to the Kc constructor.
 
Campbell Ritchie
Marshal
Posts: 69814
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can set the focus at all, you do it by clicking on the panel. Probably lots of other ways.
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please use the button for code tags, don't write them by hand; I had to remove the "" to make them work.
I added SY_O.print(keyTyped); to your keyTyped() method, which is a good way to see whether it works. It didn't. It says in the tutorials link that you have to implement a setFocusable method, so I added setFocusable(true) to the Kc constructor.


Thanks Campbell, that did the trick. Sorry about not using the button for code tags; I've never done that before.

Is that better?
 
Campbell Ritchie
Marshal
Posts: 69814
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome And you can see for yourself that HelloWorld program displayed correctly.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic