views:

1642

answers:

2

How do I handle multiple key press in java?

I'm trying to write a game and need to handle multiple keys presses at once.

When I'm holding a key (let's say to move forward) and then I hold another key (eg: to turn left), the new key is detected but the old pressed key isn't being detected anymore.

+3  A: 

One way would be to keep track yourself of what keys are currently down.

When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.

Then in your game loop, you can do actions based on what's in the list of keys.

Michael Myers
That doesn't seem that hackish. (Obviously there are slight problems related to focus - I guess assume all up on gain/lose focus.)
Tom Hawtin - tackline
A: 

Generally speaking, what you are describing can be achieved using bitmasks.

Yuval A
-1 no java related answer and no help answering the question
Janusz