A question out of curiousity

I just ran into something that might be strange.

Since switching over to ubuntu, I’ve haven’t done anything directly within a KeyListeners methods, so this is the first chance I’ve had to notice something that is possibly different from java under Windows. The thing is; I can’t check it myself.

When I run the program and hold down a key, the system begins firing both keyPressed() and keyReleased() continuously. However, the way I remember it from the last time I needed to know this (under Windows), the system would only keep performing keyPressed() while the button was held, and its seems like some old code is meant to work like that (and it did do what I needed it too). So I’m wondering if this really is a difference, or am I just insane?

Yes it’s different from what it does on all the window machines I use.

Youre right.

If I remember, its KDE wich fires these KeyPressed/KeyReleased event for autorepeat.
You can use something like this to know how long a key has been hold down :


protected long leftKeyTimer = 0;
protected long leftKeyFinalTimer = 0;

// On Key Press Event:
if (leftKeyTimer == 0) {
    leftKeyTimer = System.currentTimeMillis();
}
// On Key Release Event:
leftKeyFinalTimer += (System.currentTimeMillis() - leftKeyTimer);
leftKeyTimer = 0;

// When you update your game logics:
if (leftKeyTimer != 0) {
    leftKeyFinalTimer += (System.currentTimeMillis() - leftKeyTimer);
    leftKeyTimer = System.currentTimeMillis();
}
long leftTime = leftKeyFinalTimer;
leftKeyFinalTimer  =0;
// use leftTime here

The thing is you cant rely on key release event to know when a key has been really released. If it has not been really released, a keyPress event is fired right after the KeyRelease one.
So you might also try to wait sonething like 10ms (you should check how really close in time these 2 events are fired) when you receive a keyRelease event before processing it and cancel it if a keyPress event is received during this time.

Regards,

Charly

Has this bug still not been fixed ???

It’s been there since 1.1, more than a decade! ???

o.O! wtf I don’t have this problem…

Yes. Thanks: I’ve disabled it in KDE and now I’m going to go back to the 4k competition entries which were giving me key handling problems to see whether they’re any better.