- Understanding event.key over keyCode
- Common Key Values and Their keyCode
- Detecting Key Actions
- Why Move Away from keyCode
- Conclusion
Creating a comprehensive and up-to-date list of all possible key values and their corresponding keycodes in JavaScript presents a challenge due to modern browser standards moving away from keyCode properties in favor of event.key. The use of keyCode is deprecated, as it does not provide a reliable cross-platform or cross-browser way of identifying key actions, especially considering internationalization and accessibility. The event.key property is now recommended for identifying keys because it is more readable and less dependent on the physical keyboard layout.
Below is a reference of some commonly used key values alongside their traditional keyCode values, keeping in mind that you should prioritize using event.key in new projects.
Understanding event.key over keyCode
-
event.keyprovides the value of the key pressed, making it easier to understand and maintain code. For example, pressing the A key will yield"a"or"A"depending on whether the shift key is also pressed. -
keyCode, a numerical value, is deprecated and may not be supported in future web standards. It offered less readability and was more challenging to use without referencing a list of codes.
Common Key Values and Their keyCode
Below is a list combining both event.key values for modern development practices and their corresponding (but deprecated) keyCode values for reference:
Alphanumeric Keys
- A-Z:
event.keyis"a"to"z"(lowercase without shift key, uppercase with shift key),keyCodeis65to90 - 0-9:
event.keyis"0"to"9",keyCodeis48to57for top row numbers
Special Characters (vary by keyboard layout)
- Space:
event.keyis" ",keyCodeis32 - Enter:
event.keyis"Enter",keyCodeis13 - Escape:
event.keyis"Escape",keyCodeis27
Control Keys
- Shift:
event.keyis"Shift",keyCodeis16 - Control (
Ctrl):event.keyis"Control",keyCodeis17 - Alt:
event.keyis"Alt",keyCodeis18 - Backspace:
event.keyis"Backspace",keyCodeis8 - Tab:
event.keyis"Tab",keyCodeis9
Arrow Keys
- Left:
event.keyis"ArrowLeft",keyCodeis37 - Up:
event.keyis"ArrowUp",keyCodeis38 - Right:
event.keyis"ArrowRight",keyCodeis39 - Down:
event.keyis"ArrowDown",keyCodeis40
Function Keys
- F1-F12:
event.keyis"F1"to"F12",keyCodeis112to123
Detecting Key Actions
When detecting key presses, rely on event.key:
document.addEventListener('keydown', (event) => {
console.log(`Key pressed: ${event.key}`);
});
Why Move Away from keyCode
The movement away from keyCode towards event.key was motivated by several factors:
-
Readability:
event.keyis much more understandable without referring to documentation. -
Internationalization:
event.keyis less dependent on the physical location of keys, making it more suitable for international applications. -
Deprecation and Future Support: Modern web standards recommend against using
keyCode, as it may not be supported in future browsers and devices.
Conclusion
While historical keyCode values are part of JavaScript's legacy in handling keyboard events, modern web development practices encourage the use of event.key for a more readable, reliable, and future-proof approach. Transitioning to event.key not only aligns with current web standards but also enhances the accessibility and internationalization of web projects. Remember, the key to successful keyboard event handling is understanding user needs and browser compatibility to create inclusive and functional web applications.
Interested in proving your knowledge of this topic? Take the JavaScript Fundamentals certification.
JavaScript Fundamentals
Showcase your knowledge of JavaScript in this exam, featuring questions on the language, syntax and features.
$99