mihsathe mihsathe - 1 year ago 133
Javascript Question

Detecting arrow key presses in JavaScript

How do I detect when one of the arrow keys are pressed? I used this to find out:

function checkKey(e) {
var event = window.event ? window.event : e;

Though it worked for every other key, it didn't for arrow keys (maybe because the browser is supposed to scroll on these keys by default).

Answer Source

arrow keys are only triggered by onkeydown, not onkeypress

keycodes are:

  • left = 37
  • up = 38
  • right = 39
  • down = 40
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download