Reputation: 901
So I draw an object on the screen at objectx, objecty and increment objectx when right arrow is pressed, moving the object to the right. The problem I'm running into is that if I hold the right arrow key down it increments once, pauses, and then increments repeatedly. My question is why does it do this, and how can I make the object move fluidly without that initial pause?
$(window).keydown(function(e) {
if(e.keyCode == 39) {
objectx++;
}
}
Upvotes: 5
Views: 1498
Reputation: 1171
There is nothing wrong with the code - the auto-repeat key behavior is O/S + Browser dependent, and each user will have a different setting for how long a key has to be pressed continuously before it is registered as repeating key press, and at which rate, too. This behavior is intended to avoid errant multiple key presses.
Code-wise, the keyDown
event is being captured flawlessly, it is the browser that does not fire those events.
Upvotes: 0
Reputation: 4067
Make an interval that increases the objects, on key down, and stop the interval on keyup. (Save the interval ID somewhere, also make sure to not make the interval twice when it's already there)
Upvotes: 3