useKeyboard
A hook that handles keyboard events with a simple key-to-handler mapping.
useKeyboard
The useKeyboard
hook provides a simple and declarative way to handle keyboard events in React components. Instead of manually setting up event listeners, you can define a mapping of keys to handler functions, making your keyboard interaction code more readable and maintainable.
Press arrow keys, space, or enter
Installation
Install the useKeyboard hook using:
File Structure
Parameters
Prop | Type | Default |
---|---|---|
handlers | Record<string, (event: KeyboardEvent) => void> | Required |
options | Object | {} |
Options
Prop | Type | Default |
---|---|---|
target | Window | HTMLElement | null | window |
event | 'keydown' | 'keyup' | 'keypress' | 'keydown' |
enabled | boolean | true |
Examples
Game Controls
Implement keyboard controls for a simple game:
Use arrow keys to move the square
Use +/- keys to resize
Modal with Keyboard Navigation
Create a modal that can be closed with the Escape key:
Form Keyboard Shortcuts
Add keyboard shortcuts to a form:
Supported Keys
This hook works with standard key names as provided by the event.key
property in the browser. Here are some commonly used keys:
- Arrow keys:
arrowup
,arrowdown
,arrowleft
,arrowright
- Special keys:
enter
,tab
,escape
(oresc
),space
,backspace
- Letter keys:
a
,b
,c
, etc. - Number keys:
0
,1
,2
, etc. - Symbol keys:
+
,-
,/
, etc.
For a complete list of key values, refer to the MDN KeyboardEvent key reference.
Modifier Keys
The hook passes the full KeyboardEvent
to your handlers, so you can check for modifier keys:
Use Cases
- Keyboard Shortcuts: Implement application-wide or component-specific shortcuts
- Games: Handle directional controls for games
- Accessibility: Enhance keyboard navigation
- Modal/Dialog Controls: Close with Escape or confirm with Enter
- Form Navigation: Move between fields with keyboard
- Media Controls: Play/pause/skip with keyboard
- Slideshows: Navigate between slides with arrow keys
- Command Palettes: Trigger command interfaces with keyboard
- Drawing Applications: Tool selection with keyboard
- Text Editors: Key bindings for editing actions
Multiple Key Combinations
For detecting multiple keys pressed simultaneously (beyond just modifier keys), you can track key state:
Accessibility
When implementing keyboard shortcuts, consider these accessibility guidelines:
- Use common keyboard patterns that match user expectations
- Provide visible indicators for available keyboard shortcuts
- Avoid overriding browser or screen reader shortcuts
- Make sure all functionality is also available without keyboard shortcuts
- Test with screen readers and keyboard-only navigation
- Consider adding a keyboard shortcuts help section
Browser Compatibility
The hook uses the standard KeyboardEvent
API which is well-supported across modern browsers. The event.key
property (used for key identification) is supported in all major browsers.
Performance Considerations
- Consider performance implications when handling fast key presses (e.g., in games)
- For complex key handling, such as in games, you might want to implement your own key state tracking for better performance
Best Practices
- Use
preventDefault()
when implementing shortcuts to avoid triggering browser defaults - Focus on the target element when using the hook with specific DOM elements
- Consider conflict management when implementing global shortcuts
- Document keyboard shortcuts for users
- Group related shortcuts in a consistent manner
- Consider keyboard layout differences for international users
- Use descriptive names for handler functions
- Disable handlers when components are inactive to prevent interference