Numpad key codes

Numpad digits send event.code "Numpad0" to "Numpad9", event.key "0" to "9" and keyCode 96 to 105; the operators send 106 to 111 and Num Lock 144.

Every value at a glance

Values for Number pad
Keyevent.codeevent.keykeyCodekeyCode on a Macevent.location
Numpad 0Numpad00963 (Number pad)
Numpad 1Numpad11973 (Number pad)
Numpad 2Numpad22983 (Number pad)
Numpad 3Numpad33993 (Number pad)
Numpad 4Numpad441003 (Number pad)
Numpad 5Numpad551013 (Number pad)
Numpad 6Numpad661023 (Number pad)
Numpad 7Numpad771033 (Number pad)
Numpad 8Numpad881043 (Number pad)
Numpad 9Numpad991053 (Number pad)
Numpad MultiplyNumpadMultiply*1063 (Number pad)
Numpad PlusNumpadAdd+1073 (Number pad)
Numpad MinusNumpadSubtract-1093 (Number pad)
Numpad DecimalNumpadDecimal.1103 (Number pad)
Numpad DivideNumpadDivide/1113 (Number pad)
Numpad EnterNumpadEnterEnter133 (Number pad)
Num Lock / ClearNumLockNumLock144120 (Standard)

keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.

Press it here

Click here, then press a number pad key

Using only a keyboard? Press Esc, then Tab, to leave the key box.

Code to react to Numpad 0

Recommended: match event.key
document.addEventListener('keydown', (event) => {
  if (event.key === '0') {
    // Your code here
  }
});
Match the key's place: event.code
document.addEventListener('keydown', (event) => {
  if (event.code === 'Numpad0') {
    // Your code here
  }
});
Old code that still uses keyCode (deprecated)
document.addEventListener('keydown', (event) => {
  if (event.keyCode === 96) {
    // Your code here
  }
});

Which value to use

For typed digits and operators, check event.key. It is “1”, “+” or “*”, the same as on the main keys, so one check covers both.

Use event.code (Numpad1, NumpadAdd) only when the number pad must do something of its own, such as moving in eight directions in a game. event.location is 3 for number pad keys, another way to tell them apart.

keyCode is 96 to 105 for the digits, then 106 for *, 107 for +, 109 for -, 110 for the decimal key and 111 for /. The number pad’s Enter sends 13 like the main Enter.

What trips people up

With Num Lock off, Numpad4 sends ArrowLeft

On Windows and Linux, the digit keys turn into navigation keys when Num Lock is off. Numpad4 then sends event.key “ArrowLeft” and keyCode 37, but event.code stays “Numpad4”. Numpad0 sends “Insert”, Numpad5 “Clear” (keyCode 12) and the decimal key “Delete”. If digits are missing, check event.getModifierState('NumLock') and ask the user to turn Num Lock on.

Num Lock sends keyCode 12 on a Mac

Mac keyboards have a Clear key in that place, and the number pad always types digits. It sends event.code “NumLock”, but event.key “Clear” and keyCode 12 instead of 144.

The decimal key types a comma

On German and many other layouts, NumpadDecimal types “,”. event.key is “,” while keyCode usually stays 110. If you read numbers, accept both “.” and “,”.

A Mac number pad has an = key

Full-size Apple keyboards add an = key: event.code “NumpadEqual”, event.key “=”. It isn’t in the table above, because PC keyboards rarely have it. Its keyCode is 187 in Chrome and Safari on a Mac and 61 in Firefox (on Windows, 12), so match it by key or code.

Many keyboards have no number pad

Laptops, compact keyboards and phones have none. Never require number pad keys: accept the top-row digits too, which a check on event.key does. Some laptops hide a number pad in the letter keys, switched on with Num Lock; while it is on, those letters send number pad codes.