Overview
isHotkeyPressed is a utility function that allows you to check whether a specific key or combination of keys is currently being pressed. This is useful for conditional logic based on the current keyboard state.
This function tracks the global keyboard state and works independently of React components. It can be called anywhere in your application.
Signature
Parameters
The key or keys to check. Can be a single key string, a comma-separated string of multiple keys, or an array of key strings.Keys are case-insensitive and will be automatically trimmed of whitespace.
The delimiter used to split the key string if provided as a string. Only used when
key is a string, not an array.Returns
Returns
true if all specified keys are currently pressed, false otherwise.Usage
Basic Key Check
Multiple Keys
Check if multiple keys are pressed simultaneously:Custom Delimiter
Conditional Behavior
UseisHotkeyPressed to modify behavior based on modifier keys:
Important Notes
The function is case-insensitive. Both
isHotkeyPressed('Shift') and isHotkeyPressed('shift') will work identically.Key names are automatically trimmed of whitespace, so
'ctrl, shift' and 'ctrl,shift' are equivalent.How It Works
The library maintains a global set of currently pressed keys by listening tokeydown and keyup events on the document. The set is automatically cleared when:
- The window loses focus (
blurevent) - The context menu is opened (macOS meta key behavior)
- The meta key is released (macOS-specific behavior)
