Introduction to react-hotkeys-hook
react-hotkeys-hook is a powerful and flexible React hook that provides a declarative way to handle keyboard shortcuts in your React applications. With a tiny bundle size and full TypeScript support, it’s the perfect solution for adding keyboard navigation and shortcuts to your apps.What is react-hotkeys-hook?
At its core, react-hotkeys-hook provides theuseHotkeys hook that allows you to listen to keyboard events in a React-friendly way. Instead of manually adding and removing event listeners, you simply declare what keys you want to listen to and what should happen when they’re pressed.
Key Features
Declarative API
Define keyboard shortcuts directly in your components without worrying about event listener cleanup or memory leaks. The hook handles all the lifecycle management for you.Flexible Key Combinations
Support for:- Single keys:
'a','esc','space' - Modifier combinations:
'ctrl+k','meta+shift+a','alt+b' - Multiple hotkeys:
['ctrl+k', 'ctrl+p']or'ctrl+k, ctrl+p' - Key sequences:
'y>e>e>t'(like Vim) - Wildcards:
'*'to listen to any key
Scopes for Complex UIs
Organize hotkeys into scopes to prevent conflicts and enable/disable groups of shortcuts based on your application state.Focus Trapping
Limit hotkeys to specific elements by using the ref returned from the hook:Form Field Handling
By default, hotkeys are disabled when typing in input fields to prevent accidental triggers. You can customize this behavior with theenableOnFormTags option.
Runtime Key Detection
Check if a key is currently pressed anywhere in your application:Why react-hotkeys-hook?
Tiny Bundle Size
Minimal impact on your application’s bundle size with tree-shaking support
TypeScript First
Written in TypeScript with full type definitions for the best developer experience
Framework Agnostic
Works with any React application - CRA, Next.js, Vite, Remix, and more
Production Ready
Battle-tested in production applications with extensive test coverage
Common Use Cases
- Keyboard navigation: Navigate through your application using arrow keys
- Command palettes: Trigger search or command interfaces with shortcuts like
cmd+k - Editor shortcuts: Build rich text editors or code editors with familiar shortcuts
- Accessibility: Provide keyboard alternatives to mouse-only interactions
- Power user features: Add advanced shortcuts for experienced users
- Gaming and interactive UIs: Handle complex key combinations and sequences
Next Steps
Installation
Get started by installing react-hotkeys-hook in your project
Quick Start
Learn the basics with a hands-on example
