Modern Web Developer Guide: event.key vs event.code vs keyCode
For over two decades, web developers relied on event.keyCode and event.which to capture keyboard interactions. However, the W3C DOM Level 3 Events specification formally deprecated these properties due to inconsistent cross-browser values and lack of international keyboard layout adaptability.
event.key (Value-Oriented)
Represents the value of the key pressed, taking into account Shift modifiers, CapsLock, and international layouts. Pressing the 'A' key yields "a", but when holding Shift, it becomes "A". Ideal for form inputs, shortcuts, and text editor manipulation.
event.code (Position-Oriented)
Represents the physical key location on the keyboard, independent of the operating system's selected language or active layout. For example, the top-left letter key always emits "KeyQ" on a standard US QWERTY keyboard, but also on AZERTY keyboards where the engraved letter is 'A'. Essential for browser games (WASD navigation).
e.keyCode === 13. Prefer modern standard syntax: e.key === 'Enter'. This avoids unexpected bugs on non-US keyboards and mobile browsers.