I have an issue with my EventHandler for Key events in my game.

The specifics are in this forum thread: http://www.java-gaming.org/topics/problems-with-key-event-handling/37998/view.html

Basically, the issue is that a KeyReleasedEvent is fired in a very inconvenient way. I recently switched from polling to interrupting to take key input, so I'm hoping somebody has had this problem before and knows an elegant way to solve it.

I can provide more code and details if needed. Thank you for any help!

In short, you want to track whether each key is pressed separately. I can't speak to the engine you're using, but normal java events for a key being released tell you which key with getKeyChar() or getKeyCode(), even for key released events.

You can make an int variable for the handler object and set it equal to the key code in the keyPressed handler. Then, in your keyReleased handler, only stop the movement if the current key code equals the value of the int that holds the last keycode pressed.

  • Based on some advice I got on reddit i'm going to go back to polling for this. Their reasoning was that polling is superior for operations that rely on frame-by-frame changes whereas interrupting is better for stuff like press 'i' to bring up an inventory menu. – DayTripperID Jan 19 '17 at 22:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.