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

KeyEvent Problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic