• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Keyboard input and the Swing Timer Class

This topic is 6541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

  • Advertisement