KeyListener problems

Well, my KeyListener works, but I think its a bit odd how it does work. I just checked, and whenever I press a key, the keyPressed() method is called once, waits a bit, and then it continuously gets called. I know this is normal.

But, I just checked, and as keyPressed is called continuously, keyReleased is called contiuously too. It looks something like this:

keyPressed
–wait for a second–
keyPressed
keyReleased
keyPressed
keyReleased

Is that normal that keyReleased is being called? Sometimes, it comes up with problems, like if I am travelling down right in my game, a keyPressed event is skipped, so it no longer travels in a diagonal direction, either down or right. However, this happens randomly.

It only lasts for a very little time, but its very noticeable. Any tips on how to fix this? I think it might have something to do with me being on Ubuntu, and I’m on OpenJDK.

Unfortunately that is the key-repeat from the OS, so nothing java can do about :confused: Afaik, the only way to overcome this issue is to use a lib with native bindings like JInput.

huh, I guess I’ll do that.

You could try key polling like what I did here:

net.phatcode.rel.utils/Keyboard class

http://rel.phatcode.net/junk.php?id=138

I’ll try that, but it still will have the same problem. keyPressed and keyReleased are constantly called(one after eachother), and sometimes the keyPressed event misses a few calls, and the net effect is that the key is released.

I’ll see though, and update you if it works :slight_smile:

Same problem I had with the callback mechanism of keylisteners. That’s why I poll my input. It’s easier to use too.

Alright, I’m adding it in right now. Also, wouldn’t it be better if you just got an array for having the keyPress array? Isn’t it pointless to have a keysReleased array since keyPress and keyRelease both have opposite values(true and false), so you can just call !keysPressed[KEY] to get if it is released?

You could try that but it won’t work with just one array. Hence the scanKeys() method.

Dang, that helped me alot. Not only am I taking the input, I’m replacing my whole frame control logic. Thanks!