[java] Keyboard input and the Swing Timer Class

Started by
2 comments, last by Jerry Lynn 24 years, 1 month ago
I am attempting to perform certain actions in my game based on the combinations of the keys currently pressed on the keyboard. The event method of detecting keyboard input doesn''t seem to be what I need in that I need to be able to detect two or more keys pressed simataneously; not merely a key with a modifier (such as shift, alt, and control). As an example I need to able to detect that the ''up arrow'', the ''left arrow'' and any other letter key are pressed at the same time. I fire off my detection method based on a Swing Timer, but I do not know how to determine the current state of the keyboard in this method. The concept seems simple enough (I have implemented it in DirectX) but I can not find any references or posts on how to do this. Is my basic concept off here? I would appreciate any pointers some one could provide. Thanks,
Advertisement
Since the API exposes the keyPressed and keyReleased events, I''m sure that they come to your event handler, regardless of whether another key is pressed down at the same time or not. Because of this it looks like you''ll have to queue events up in a set or something to that order.

JoeG
joeG
I was afraid of that...

Thanks for the direction - you have saved me from wasting my time on a dead end...
Why on earth are you talking about event queues in this case :o ?

In order to read the keyboard from the Swing thread and to be able to detect multiple simultaneous keys down, you''ve just got to have four flags (fUp, fDown, fLeft & fRight). Then in your keyDown and keyUp handlers you modify these flags according to received events. And in the Swing timer triggered part of your code you read these flags and act appropriately.

Simple, eh?
-Pasi Keranen

This topic is closed to new replies.

Advertisement