Pause while holding a key

So I draw an object on the screen in the object x, objecty and increment objectx when I press the right arrow, moving the object to the right. The problem I am facing is that if I hold the right arrow key, it increases once, pauses, and then increases several times. My question is, why does he do this, and how can I make an object move without this initial pause?

$(window).keydown(function(e) { if(e.keyCode == 39) { objectx++; } } 
+4
source share
2 answers

Make an interval that enlarges objects by pressing down and stops the interval when you press a key. (Save the interval id somewhere, also remember to do the interval twice when it's already there)

+3
source

There is nothing wrong with the code. The behavior of the auto-repeat key depends on O / S + Browser, and each user will have a different setting for how long the key must be pressed continuously before it is registered as a repeating press key and at what speed too. This behavior avoids the erroneous keystrokes of several keys.

According to the code, the keyDown event keyDown recorded flawlessly, this browser does not keyDown these events.

0
source

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


All Articles