Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Up, Down, Left, Right Arrow KeyCodes always the same?

Tags:

javascript

As the title suggests, in my code I use the following codes:

  • Left: 37
  • Up: 38
  • Right: 39
  • Down: 40

And check for those key codes to determine my action. My question is, do those always remain the same? If I were to use a DVORAK keyboard, or a non-English keyboard, would those key codes remain the same?

Along the same line, is there a preferred method for detecting those keystrokes?

Currently, I do it as follows:

    var Key = {         _pressed: {},         LEFT: 37,         UP: 38,         RIGHT: 39,         DOWN: 40,          isDown: function (keyCode) {             return this._pressed[keyCode];         },          onKeydown: function (event) {             this._pressed[event.keyCode] = true;              if (Key.isDown(Key.UP))                 //do up action             else if (Key.isDown(Key.DOWN)) {                 //do down action             }             delete this._pressed[event.keyCode];         }     }; 
like image 648
wjhguitarman Avatar asked Dec 31 '13 21:12

wjhguitarman


People also ask

What are the four types of arrow keys?

Alternatively referred to as cursor keys, direction keys, and navigation keys, the arrow keys are usually located between the standard section and the numeric pad on computer keyboards. It is made up of four keys: the left arrow (back arrow), up arrow, down arrow, and the right arrow (forward arrow).

What is e keycode === 13?

key 13 keycode is for ENTER key.

How do I know which arrow key to press?

To detect the arrow key when it is pressed, use onkeydown in JavaScript. The button has key code. As you know the left arrow key has the code 37. The up arrow key has the code 38 and right has the 39 and down has 40.


1 Answers

Yes, arrow key keycodes are always the same, regardless of the keyboard layout.

I regularly use different keyboard layouts (Dvorak, Russian, Ukrainian, Hebrew, Spanish) and I have tried all of them in JavaScript and they give consistent results for the arrow keys.

like image 128
Peter Olson Avatar answered Sep 23 '22 23:09

Peter Olson