Javascript keyup does not work properly, it is executed in cases where I did not remove the finger from the button

I am trying to create a simple javascript game and I am fixated on how to handle keys.

A small example:

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

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

If I press and release the button instantly, I get a warning that should be expected, but when I press and hold the button, I get a short pause and then a series of warning windows, the expected result is that I only get a warning window when I remove the finger from the button.

I think this is because when I press and hold the button in the text area, for example, I get one character, a short pause, and then a series of characters: ddddddddddddddddd.

I believe that you can get around this or do it more correctly or anything, starting with this game, for example: http://bohuco.net/testing/gamequery/pong.html the seams should not be affected by this. But I notice that if I try jquery keyup demo (api.jquery.com/keyup/), I get the same problem.

How can I implement basic game key event processing?

+3
source share
1 answer

the example you give has a solution - why not follow it?

It uses a timer after keyDown, and its call does not call the function that performs the task. And this is a trick, you should use a timer.

.

0

Source: https://habr.com/ru/post/1743679/


All Articles