Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Recognizing Keys Pressed with a JPanel  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 198
1
  • 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.?
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • 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: 198
1
  • 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
Sheriff
Posts: 53750
127
  • 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
Sheriff
Posts: 53750
127
  • 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: 198
1
  • 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
Sheriff
Posts: 53750
127
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!