Home·Key codes

code and keyCode for 120 keys

The values a KeyboardEvent gives you. code is the physical position and never changes with layout; key is what actually gets typed, and that does.

code versus key

code is the physical position, the same on a Korean or French keyboard. key is what gets typed and changes — the same spot gives a, A or ㅁ. Use code to ask which spot was pressed, key to ask what was entered.

keyCode has been dropped from the standard. Write new code against code or key; the numbers here are for reading old code.

Letters26

The position is fixed as KeyA. Even when a Hangul ㅁ is typed, the code is still KeyA.

Digits10

The number row. Its keyCodes differ from the same digits on the numpad.

Punctuation12

This is where keyCode values part ways between browsers.

Numpad16

location is 3, which is what tells it apart from the number row.

Function keys20

F1 is 112, F2 is 113, and so on in order.

Navigation8

They type nothing and only move the cursor.

Editing6

Deleting, inserting, breaking a line.

Modifiers8

They do nothing alone and ride on other keys. Left and right share one keyCode.

Locks3

Press once and the state stays changed.

System4

Keys that deal with windows and the screen.

Input method7

Keys that switch the input method — Hangul, Hanja and the Japanese conversion keys.

How to read this

  • code is the position, key is the character typed. Match on code for shortcuts.
  • keyCode is deprecated; use it only to read old code.
  • Left and right Shift share a keyCode and are told apart by location.
  • The 1 on the number row and the 1 on the numpad are different keys — 49 and 97.

Frequently asked questions

Q. Should I use code or key?

code when you need the position, as for shortcuts; key when you need what was typed. code survives a change of layout.

Q. Why avoid keyCode?

It was dropped from the standard and browsers disagree on the values, especially for punctuation.

Q. What is the key code for Enter?

keyCode 13, code Enter. The Enter on the numpad has code NumpadEnter but the same keyCode 13.

Q. How do I tell left Shift from right Shift?

Not by keyCode — both are 16. Use code (ShiftLeft, ShiftRight) or location (1, 2).

Q. Is code the same on a Korean keyboard?

Yes. Pressing the spot that types ㅁ still reports KeyA, because code names the position.