How do you manage key release events?

I’m trying to figure out how to send a key release event from a component. The KeyListener class has keyPressed, which includes information about which key was pressed, but no keyRelease function. There is keyStateChanged, but the problem with that is it oddly does not include the KeyPress data.

So do I need to keep track of all the keys that are pressed with keyPressed, then loop though those values on keyStateChanged to check which ones are up? Seems inefficient. Is there a better way?

I also have this problem when try to implement a simple game engine.
I think keyStateChanged method should contain the KeyPress data. I also need this badly…

I made a class to keep a register of every key that is down (which is easy to know with the help of the keyPressed() function.

Then when a key is released it calls to keyStateChanged(false), and I look if any key which was down has been released with KeyPress::isKeyCurrentlyDown().

I also include modifierKeysChanged in that list, but that’s one its easier… unless you want to know which Ctrl or Shift (left/right) was down/up, that you will need to hack a bit, what I did in Windows too.

When a key is released , you should loop through these keys which have been pressed down, which I think is inefficient.

This is the problem I want to fix. I do not want to loop through the keys and call api to check each key that has been pressed to see whether they are released.

Inefficient? How many keys are you able to keep hold at the same time? 10? Even 100 would be a very short loop. I was there, trust me, it’s not an optimal solution but you don’t lose any cpu cycle there.

And remember: you only need to loop when you detect a key has been released. I hope it helps!