Apparently this question has been asked before and before and before, but either the answers are not solving the problem or use a language not understandable by me.
I have to register a keyboard action using JComponent.WHEN_IN_FOCUSED_WINDOW, so a KeyListener is not an alternative. I have to track pressing down and releasing the key, say a cursor key. I cannot have multiple press/release firings due to some built-in key repeat. I really want to know when the key is physically pressed and depressed. E.g., start a timer when pressed, stop the timer when released (and do not start and stop the timer dozens of times because of the virtual key repeats).
I believe this worked correctly on OS X, but I am on Linux now (OpenJDK 7).
How?
As multiple people have noted, this is an oddity (or: bug) on Linux.
The work-around goes as follows. Let's say the press action class is called
ActionPress, and the release action class is calledActionRelease.long lastWhenthat is updated inactionPerformedwithe.getWhen. They have a public methodgetLastWhenthat can be called by their counterparts.ActionReleasemaintains ajavax.swing.Timerwith a few milliseconds. It's regularactionPerformedrestarts the timer, nothing else.ActionPressin itsactionPerformedcalls the correspondingActionRelease'sgetLastWhen. If the result is identical to its ownlastWhen, ignore the action and return, otherwise perform the action.ActionReleasein the additionalActionListenerfor theTimerperforms the actual action. In itsgetLastWhenimplementation, it stops the timer before returnlastWhen.