keyCode 229 and "Unidentified" keys

keyCode 229 means an on-screen keyboard or an input method took the key; read the text from the input event instead.

Press it here

Type in the text box with your phone keyboard or an input method

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

Which value to use

When keyCode is 229, don’t act on the key. 229 is not a key: it means the browser got the key press through an on-screen keyboard or an input method (IME), which decides the text itself and doesn’t say which key it was (with an input method on a computer, code may still name the physical key, but the text isn’t decided yet). What arrives depends on how the user types:

Typing with What arrives on keydown
An Android keyboard key “Unidentified”, code “”, keyCode 229, isComposing often true
Chinese, Japanese or Korean input (IME) key “Process” or the real key, code the real key, keyCode 229, isComposing true
An iPhone or iPad keyboard key the real key, code often “”, keyCode the real number, isComposing false
A hardware keyboard, no input method key, code and keyCode all real, isComposing false

What trips people up

Shortcuts fire while someone types Japanese, Chinese or Korean

The keys that build a character still send keydown events. Skip them in shortcut handlers, and read the finished text from the input or compositionend event:

input.addEventListener('keydown', (event) => {
  if (event.isComposing || event.keyCode === 229) return;
  // your shortcuts
});

input.addEventListener('beforeinput', (event) => {
  // event.inputType: 'insertText', 'deleteContentBackward', 'insertLineBreak', ...
  // event.data: the text being added, or null
  // Blocks typed non-digits only: paste, drop and composition take other paths,
  // so check the field's value again on input or change.
  if (event.inputType === 'insertText' && /\D/.test(event.data ?? '')) {
    event.preventDefault();
  }
});

Check both values: Safari sends the Enter that confirms a character with isComposing false but keyCode 229.

Every key on an Android phone is “Unidentified”

Gboard, SwiftKey and other Android keyboards send most keys as keyCode 229 and event.key “Unidentified”. Some send a real Backspace or Enter, but that differs between keyboards and versions, and some taps arrive with no keydown at all. Don’t read keys there: read inputType and data from beforeinput or input. A deletion has inputType “deleteContentBackward” and data null.

preventDefault() on keydown doesn’t stop the text

The text doesn’t come from the keydown, so cancelling it changes nothing. Cancel the beforeinput event instead, as in the example. One exception: while the keyboard is composing a word (inputType “insertCompositionText”), Chrome and Firefox don’t let you cancel it. Fix the field’s value in the input event or at compositionend.

event.code is empty on an iPhone

iPhones and iPads usually send the real event.key and keyCode, but event.code is often “”. Match on-screen keys with event.key.

It works with a USB keyboard but not on the screen keyboard

A hardware keyboard on a phone sends real keys (unless an input method is on), so testing with one hides the problem. Test with the keyboard on the screen, or use the check above.