Shift key code

Both Shift keys send event.key "Shift" and keyCode 16. Only event.code and event.location tell left from right.

Every value at a glance

Values for Shift
Keyevent.codeevent.keykeyCodeevent.location
Left ShiftShiftLeftShift161 (Left side)
Right ShiftShiftRightShift162 (Right side)

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 Shift

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

Code to react to Left Shift

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

Which value to use

To know if Shift is held during another key, check event.shiftKey. It is true on every key event while either Shift is down, and you don’t have to track the Shift key yourself.

To react to Shift itself, check event.key === 'Shift'. Both keys send it, with keyCode 16. Use event.code (ShiftLeft or ShiftRight) or event.location (1 left, 2 right) when the two must do different things.

On the Shift key’s own events, shiftKey is true on keydown and false on keyup, because it shows the state after the press or release.

What trips people up

Ctrl+Shift+Z doesn’t match event.key === ‘z’

Shift changes the other key’s event.key: “z” becomes “Z”, “1” becomes “!”. It doesn’t change event.code (KeyZ, Digit1) or keyCode (90, 49). For letter shortcuts with Shift, compare in lower case or use code:

if (event.ctrlKey && event.shiftKey && event.key.toLowerCase() === 'z') redo();

Shift+2 types @ for you but “ for someone else

The symbol on a Shift + number key depends on the layout: Shift + 2 is “@” on a US keyboard and ‘“’ on a UK one, and AZERTY needs Shift to type the digits themselves. If your shortcut is a symbol, check event.key for the symbol and ignore shiftKey. If it is a key position, check event.code.

A Shift key stays “held” after both were pressed

On Windows, when both Shift keys are down, the system may report only one release, so the keyup of one Shift can go missing. If you track each side, clear both when any Shift keyup arrives, or check event.shiftKey on the next key event to fix your state.

Shift held while the window loses focus

The keyup then goes to another window. Reset your Shift state on the window’s blur event, or trust event.shiftKey on the next key or mouse event instead of your own flag.

Shift with the mouse and on phones

Mouse and pointer events also carry shiftKey, so Shift + click (select a range) needs no key listener. Don’t expect a Shift key event from a phone’s on-screen keyboard: Android often reports keys as keyCode 229, and the capital letter just arrives as text.