1
\$\begingroup\$

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!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Skywarp
    Jan 19, 2017 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .