Javascript keyup doesn't work as expected, it executes in instances where I have not removed my fing

Posted by Binni on Stack Overflow See other posts from Stack Overflow or by Binni
Published on 2010-05-02T05:20:34Z Indexed on 2010/05/02 5:27 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

I'm trying to create a simple game in javascript and I'm stuck on how to deal with keys.

Small example:

function keyUpEvent(event) {
    alert(event.keyCode);
}
window.addEventListener("keyup", keyUpEvent, false);

I'm running Ubuntu 9.10 and testing it in Firefox 3.5 and Chromium.

If I press and release a button instantly I get an alert, which is to be expected, but when I press and hold a button I get a small pause and then a series of alert windows, the expected result is that I only get an alert window when I remove my finger from a button.

I reason it has something to do with the fact that when I press and hold a button in a text area for example I get one character, small pause, and then a series of characters: dddddddddddddddd.

I believe it's possible to get around this or do it more right or whatever since this game for example: http://bohuco.net/testing/gamequery/pong.html seams not to be affected by this. But I notice if I try out the jquery keyup demo ( api.jquery.com/keyup/ ) I get the same problem.

How can I implement basic game key event handling?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about onkeyup