Javascript holding down arrow keys?

I have this script to call some javascript. But the script does not support holding down arrow keys. How can I do this job when I hold the arrow keys.

document.onkeyup = KeyCheck;       
function KeyCheck()

{

var KeyID = event.keyCode;


switch(KeyID)

{

  case 37:

  right('img'); document.getElementById('img').src = 'guyl.png';

  break;

  case 38:

  up('img');

  break

  case 39:

  left('img'); document.getElementById('img').src = 'guyr.png';

  break;

  case 40:

  down('img');

  break;


 }

}
+3
source share
3 answers

it should be:

document.onkeydown = KeyCheck;

onkeypress: invokes JavaScript code when a key is pressed

onkeydown: invokes JavaScript code when the key is held (but not yet released)

onkeyup: Invokes JavaScript code when the key has been released after being pressed.

+6
source

You just need to handle the event onkeydown.

0
source
  • , arg

    function KeyCheck () {

    var KeyID = event.keyCode;

    ...

    }

  • , onkeypress event , , .

0

All Articles