How to know when a user has really released a key in Java?

This could be problematic. I can’t remember for sure (it’s been a long time), but it’s likely the repeating-key feature (which is handled by the underlying operating system, not Java) isn’t providing enough information for the JVM developer to distinguish those additional key events from the ‘real’ one. (I worked on this in the OS/2 AWT back in 1.1.x by the way).

From the javadoc for KeyEvent:

“Key pressed” and “key released” events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don’t generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code.

As I recall from doing this in OS/2 (which at the time still had only the 2-event up/down flavor of keyboard handling like older versions of Windows, not the 3-event up/down/char flavor you get in more modern versions), I didn’t report KeyReleased events any differently if the key was just being held down and the events auto-generated; but I suspect OS/2 didn’t even report that information to me (can’t remember for sure). We used the Windows reference JVM from Sun as our guide for developing our AWT – so I suspect if it were possible to report this information there, I’d have at least seen it on their end.

Leave a Comment