Trigger Auto-Repeat Javascript keydown event

800 Views Asked by At

When you hold down a key, the javascript keydown event is triggered repeatedly while the key is still down. I want to programmatically simulate this effect. I have tried the following code but it only triggers the keydown event only once:

e = jQuery.Event("keydown"); // define this once in global scope
e.which = 38; // Some key value
$("input").trigger(e);

Any help is appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

This should call .trigger() every 50 ms forever...

e = jQuery.Event("keydown"); // define this once in global scope
e.which = 38; // Some key value

setInterval(function() {
  $("input").trigger(e);
}, 50); // Change 50 to whatever you like.