i have searched a long time for this kind of problem.It occurs when i'm pressing a button and hold it (e.g. moving forward) and then pressing a another button (e.g. moving backward).My program recognized the event from the second hit, but instead of moving the character / camera stops.It only starts moving again when i'm pressing the second button again and hold it.

well, i expect something like moving 'down', after i pressed the down button. It seems like an "input lock".All i want is: always perform the specified action when the button is pressed and cancel the current action.

You want to move in the direction of the last arrow key pressed. Thus you want to loop through every key event before calling setMovement. Set a lastKey variable to keep track of the last direction key pushed. If you release that last key, then you don't want to move, so unset the direction variable.

@OPPressing the down button with the up button already down doesn't release the up button. Your code should work. Have you tried putting SOP's to see if any key-release events are dispatched?

@Best Username EverForgot 'case 0: cam.setMovement(FirstPersonCamera.CameraMovement.NONE)' in the second switch statement However, there isn't much point in doing that since OP's code should work originally.

Oh. You're right about the case 0. I don't think his code works, though. Pressing W, pressing S, and releasing W will not make the camera move down. Releasing W cancels the S key that came later. Also pressing S then space, for example, would cancel the S key press.

Oh the OP meant that if he released the first key after the second key, then the movement stops! The only way to fix that is to use polling, not event-based movement. Each tick you call Keyboard.isKeyPressed(int key) to check if that key is down and act accordingly.

1) press 'W' and hold it --> character moves forward2) press 'A' and hold it --> now, character moves leftward3) release 'A' (only 'W' is now pressed) --> character stops moving, but 'W' is pressed, so i expect that he's moving forward

@Best Username Ever:Thanks, but this workaround doesnt help with this problem.

@ra4king:Each tick i tried to check Keyboard.isKeyPressed(int key), but this doesnt work neither =/

In that case, you need to keep track of which keys are pressed. You can't use polling alone if you want that to work correctly because your if statements would have to be in a certain order and the final true if statement would override the rest. You should use a List/array to store direction keys at the end of the list as they're pressed and removed as they are released. Whatever is at the "top" of the list should be the direction you move in. Set movement to NONE if the list is empty.

Do you only want to move in the direction of the most recently pressed key or does it make sense to move in one of 8 (plus one/NONE) directions depending on which keys are pressed? A + W = FORWARD_LEFT, A + D + S = DOWN, A + D + S + W = NONE, etc.

Do you only want to move in the direction of the most recently pressed key or does it make sense to move in one of 8 (plus one/NONE) directions depending on which keys are pressed? A + W = FORWARD_LEFT, A + D + S = DOWN, A + D + S + W = NONE, etc.

i would like to press max. 2 buttons and perform an action like "A + W = FORWARD_LEFT". i think i can do this by popping down two instead of one entries from my "button-pressed-stack" right?

Yep. That's one way. But the correct operation is peek, not pop. You don't want to remove them because then a "press W, press D, release D, press A" sequence would result in just left instead of forward/left.

There are a number of ways you can handle keyboard input in situations like this. One way is too track the order of events and let more recent events cancel old ones like the original poster describes. Another is to let two keys cancel each other out. (if A and not B - if B and not A) A third is to let one key override another based on priority. (if - else if - else)

Note that if you want to do the first method you need to track events. Polling won't work*. Personally, I prefer the second method because it's more intuitive to use. Arrow keys should cancel each other out like two equal forces pulling in opposite directions do. The camera should move at a consistent rate as long as the key is held down. R4king is correct about polling if you want the second or third method (just replace the else ifs with ifs to change between one type or the other.)

*Technically the keyboard is being polled no matter what form you get the data in, but polling at the same rate as your game's update cycle might be too slow to pick up quick key presses. It matters for other quick buttons presses, but not holding down a key to move around.

I assume you can combine the two methods in LWJGL, but even if that weren't the case I would still emulate polling a set of keys once per turn and write my own isArrowKeyPressed method. If you can get the simplicity of isKeyPressed without having to lose event information and if the "always perform the specified action when the button is pressed and cancel the current action" isn't essential, then definitely consider the other methods just for camera controls.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org