• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyEvent Problem

 
Steffen Schmitt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

im doing a simple animation and it's bound to display a series of images when the arrow keys on the NUM pad are pressed. There shouln't be any problem abou this, but arrarently I don't even get the KeyEvents I hoped to process with my KeyAdapter:


I'd appreciate any suggestion about what I'm doing wrong here.

Thanks in advance,


Steffen
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the apidocs for KeyEvent:

"For key pressed and key released events, the getKeyCode method returns the
event's keyCode. For key typed events, the getKeyCode method always returns
VK_UNDEFINED"

even if you change your code to keyPressed(), you'll probably still have
problems - it will be better for you to use keyBindings

http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace keyTyped by keyReleased().

to Michael dunn,

I think adding keybindings instead of keylisteners in the above case
won't make any difference as key listeners are notified before key bindings:
The flow is like this(for JComponents):
1) key listeners
2) processComponentKeyEvent(), if the componenet overrides it
3) processKeyBindings..finally


So I think, from a design point of view, the user should actuall override processComponentKeyEvent() instead, as the given key behaviour of the componnet is tightly bound to how the component should be...just a feeling actually

Cheers,
Suraj
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Key bindings provide a high-level, loosely-coupled way to program keyboard input; they should always be the first tool the Swing programmer reaches for. Overriding processComponentKeyEvent(), on the other hand, is very low-level and tightly-coupled, and should only be done as a last resort, in my opinion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic