Question:
Does the Java Key Listener class work with 2 or more keys pressed simultaneously?
ibbolia
2010-01-29 08:42:07 UTC
I'm working on a game-style project, and need to know if this would work. Also, if Key listener can't do this, is there a class that can?
Three answers:
conventional
2010-02-02 13:06:24 UTC
Say you're programming the arrow keys (that's fairly typical for games). You'll need 4 booleans



leftArrowPressed, upArrowPressed, rightArrowPressed, downArrowPressed



Then register key down and key up events. Obviously key down events set the corresponding boolean to true, and key up events set them false.



Finally, you'll have some sort of timer loop that gets fired every so often, which checks the status of the booleans, and acts accordingly. By doing this, you can respond to multiple keys being pressed at the same time.
AnalProgrammer
2010-01-29 08:55:54 UTC
This is a question about the hardware (keyboard) not the software.

The Java Key listener will receive a message from the keyboard every time it detects a key stroke.

If two keys are hit at the same time it all depends on what the keyboard does with the keystrokes that matters.

Any good keyboard should buffer the keystrokes and send them to the operating system one at a time.



Have fun.
tretheway
2016-12-04 08:09:42 UTC
take the enter from both, use a international second matrix with x and y coordinates, once you press one button have it influence the -x coordinate and once you press the different have it influence the y coordinate, once you press them at the same time both coordinates should be affected, for down and precise do merely it x and -y


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...