Function Signature
useHotkeys hook is the primary API for binding keyboard shortcuts to React components. It returns a ref that can be attached to any HTML element to scope the hotkey to that element.
Parameters
The keyboard shortcut(s) to listen for. Can be a single string or an array of strings.Type definition:Examples:
"ctrl+s"- Single combination"ctrl+k, cmd+k"- Multiple combinations (comma-separated)["ctrl+k", "cmd+k"]- Multiple combinations (array)"a>b>c"- Key sequence
Function called when the hotkey is triggered.Type definition:Parameters:
keyboardEvent- The native browser KeyboardEventhotkeysEvent- Enhanced event object containing hotkey metadata
Configuration options for the hotkey behavior, or a dependency array for React.See the Options section below for all available configuration options.
React dependency array. When provided, the callback is memoized with these dependencies.If
options is a dependency array, this parameter should contain the Options object.Returns
A React ref object that can be attached to an HTML element to scope the hotkey to that element.Type:When the ref is not attached to any element (remains
null), the hotkey listens globally on the document.Options
TheOptions object accepts the following properties:
Determines if the hotkey is enabled. Can be a boolean or a function that returns a boolean.Type:
Enable hotkeys on form elements like inputs, textareas, and selects.Type:Pass
true to enable on all form tags, or pass an array of specific tags.Enable hotkeys on elements with
contentEditable attribute.Prevent the default browser behavior for the keyboard event.
Trigger the callback on keydown event.
Trigger the callback on keyup event.
Scope(s) for the hotkey. Only active when the scope is enabled via
HotkeysProvider.Type:Character used to split keys in combination shortcuts (e.g.,
ctrl+s).Character used to separate different hotkey combinations.
Ignore modifier keys (ctrl, alt, shift, meta) when matching hotkeys.
Custom function to conditionally ignore keyboard events.
Human-readable description of what the hotkey does. Useful for documentation or help menus.
Listen to the produced key (
event.key) instead of the key code (event.code).Use this option when you want to listen to the character being typed rather than the physical key position.
Character used to split key sequences (e.g.,
a>b>c).Timeout in milliseconds to wait for the next key in a sequence.
Custom document object to attach event listeners to. Useful for iframes or shadow DOM.
Options passed to the native
addEventListener method.Type:Custom metadata to store with the hotkey. Accessible in the callback via
hotkeysEvent.metadata.Usage Examples
Basic Usage
Multiple Key Combinations
With Options
Scoped to an Element
Key Sequences
With Dependencies
Conditional Enabling
Using the Hotkeys Event
Important Notes
By default, hotkeys are disabled on form elements. Use
enableOnFormTags to enable them on inputs, textareas, and selects.See Also
- useHotkeysContext - Access and control hotkey scopes
- useRecordHotkeys - Record keyboard input for hotkey configuration
