KeyboardEvent API Reference
The Keycode Info tool is built on the standard JavaScript KeyboardEvent API. Here's a comprehensive reference for developers.
KeyboardEvent Properties
Core Properties
key
The key value of the key represented by the event.
event.key; // "a", "Enter", "Shift", " "
Values:
- Character keys: "a", "b", "1", "2", etc.
- Special keys: "Enter", "Tab", "Escape", "Backspace"
- Modifier keys: "Shift", "Control", "Alt", "Meta"
- Whitespace: " " (space), "Tab", "Enter"
code
The physical key on the keyboard.
event.code; // "KeyA", "Enter", "ShiftLeft", "Space"
Common codes:
- Letter keys: "KeyA", "KeyB", "KeyC", etc.
- Number keys: "Digit0", "Digit1", "Digit2", etc.
- Function keys: "F1", "F2", "F3", etc.
- Arrow keys: "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"
- Modifier keys: "ShiftLeft", "ShiftRight", "ControlLeft", "ControlRight"
keyCode (Deprecated)
The numeric code of the key.
event.keyCode; // 65 for "A", 13 for "Enter"
Note: This property is deprecated but still widely supported.
location
The location of the key on the keyboard.
event.location; // 0, 1, 2, or 3
Values:
- 0: Standard key location
- 1: Left side of keyboard
- 2: Right side of keyboard
- 3: Numpad
Modifier Properties
ctrlKey
Whether the Ctrl key was pressed.
event.ctrlKey; // true or false
altKey
Whether the Alt key was pressed.
event.altKey; // true or false
shiftKey
Whether the Shift key was pressed.
event.shiftKey; // true or false
metaKey
Whether the Meta key was pressed (Cmd on Mac, Windows key on Windows).
event.metaKey; // true or false
Event Types
keydown
Fired when a key is pressed down.
document.addEventListener('keydown', (event) => {
console.log('Key pressed:', event.key);
});
keyup
Fired when a key is released.
document.addEventListener('keyup', (event) => {
console.log('Key released:', event.key);
});
keypress (Deprecated)
Fired when a key is pressed (deprecated).
// Deprecated - use keydown instead
document.addEventListener('keypress', (event) => {
console.log('Key pressed:', event.key);
});
Common Key Codes
Letter Keys
| Key | keyCode | code |
|---|---|---|
| A | 65 | KeyA |
| B | 66 | KeyB |
| C | 67 | KeyC |
| ... | ... | ... |
| Z | 90 | KeyZ |
Number Keys
| Key | keyCode | code |
|---|---|---|
| 0 | 48 | Digit0 |
| 1 | 49 | Digit1 |
| 2 | 50 | Digit2 |
| ... | ... | ... |
| 9 | 57 | Digit9 |
Special Keys
| Key | keyCode | code |
|---|---|---|
| Enter | 13 | Enter |
| Tab | 9 | Tab |
| Escape | 27 | Escape |
| Backspace | 8 | Backspace |
| Delete | 46 | Delete |
| Space | 32 | Space |
Arrow Keys
| Key | keyCode | code |
|---|---|---|
| Up | 38 | ArrowUp |
| Down | 40 | ArrowDown |
| Left | 37 | ArrowLeft |
| Right | 39 | ArrowRight |
Function Keys
| Key | keyCode | code |
|---|---|---|
| F1 | 112 | F1 |
| F2 | 113 | F2 |
| F3 | 114 | F3 |
| ... | ... | ... |
| F12 | 123 | F12 |
Modifier Keys
| Key | keyCode | code |
|---|---|---|
| Shift | 16 | ShiftLeft/ShiftRight |
| Ctrl | 17 | ControlLeft/ControlRight |
| Alt | 18 | AltLeft/AltRight |
| Meta | 91/93 | MetaLeft/MetaRight |
Browser Compatibility
Modern Browsers
- Chrome 51+: Full support
- Firefox 23+: Full support
- Safari 10+: Full support
- Edge 79+: Full support
Legacy Browsers
- Internet Explorer: Limited support
- Older Safari: Partial support
- Older Firefox: Partial support
Usage Examples
Basic Key Detection
document.addEventListener('keydown', (event) => {
console.log('Key:', event.key);
console.log('Code:', event.code);
console.log('KeyCode:', event.keyCode);
console.log('Location:', event.location);
});
Modifier Key Detection
document.addEventListener('keydown', (event) => {
if (event.ctrlKey && event.key === 'c') {
console.log('Ctrl+C pressed');
}
if (event.altKey && event.shiftKey) {
console.log('Alt+Shift combination');
}
});
Key Combination Detection
document.addEventListener('keydown', (event) => {
const modifiers = [];
if (event.ctrlKey) modifiers.push('Ctrl');
if (event.altKey) modifiers.push('Alt');
if (event.shiftKey) modifiers.push('Shift');
if (event.metaKey) modifiers.push('Meta');
console.log('Modifiers:', modifiers.join('+'));
console.log('Key:', event.key);
});
Prevent Default Behavior
document.addEventListener('keydown', (event) => {
if (event.key === 'F12') {
event.preventDefault(); // Prevent developer tools
}
if (event.ctrlKey && event.key === 's') {
event.preventDefault(); // Prevent save dialog
}
});
Tool Integration
Using with Keycode Info Tool
The Keycode Info tool provides a visual interface for understanding these APIs:
// Example: Integrate with the tool
document.addEventListener('keydown', (event) => {
// Display in tool
displayKeyInfo({
key: event.key,
code: event.code,
keyCode: event.keyCode,
location: event.location,
modifiers: getModifiers(event),
});
});
function getModifiers(event) {
const modifiers = [];
if (event.ctrlKey) modifiers.push('Ctrl');
if (event.altKey) modifiers.push('Alt');
if (event.shiftKey) modifiers.push('Shift');
if (event.metaKey) modifiers.push('Meta');
return modifiers.join(' + ') || 'None';
}
Best Practices
Event Handling
- Use keydown for most cases: Better for detecting key presses
- Use keyup for release detection: When you need to detect key release
- Avoid keypress: Deprecated and inconsistent
Key Detection
- Use code for physical keys: More reliable for key detection
- Use key for character input: Better for text input
- Check modifiers: Always check modifier key states
Performance
- Debounce events: For rapid key presses
- Remove listeners: Clean up event listeners
- Use passive listeners: When possible for better performance
Troubleshooting
Common Issues
Key Not Detected
// Check if element is focused
if (document.activeElement === document.body) {
console.log('Page is focused');
}
Inconsistent Behavior
// Use code instead of key for consistency
if (event.code === 'KeyA') {
console.log('A key pressed');
}
Modifier Key Issues
// Check modifier state
if (event.ctrlKey && event.shiftKey) {
console.log('Ctrl+Shift combination');
}
This API reference provides the foundation for understanding and implementing keyboard event handling in web applications. Use the Keycode Info tool to explore and test these APIs interactively.