javascriptjquerykeyeventautorepeat

Trigger Auto-Repeat Javascript keydown event


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.


Solution

  • 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.