useKeyUp
React hook to listen for key release events with key combinations, element scoping, and typed callbacks.
Demo
Q
W
E
A
S
D
Press and release a key
Usage
Single key or array of keys:
Key combos and modifier shortcuts:
Object form with preventDefault:
Scoped to a specific element:
Parameters
| Parameter | Type | Description |
|---|---|---|
keys | K | readonly K[] | Single key, key combination, or an array of keys / combos to listen for. |
callback | (event: KeyboardEvent, key: K) => void | Simple form. Called when a matching key is released. |
Object form second argument:
| Property | Type | Default | Description |
|---|---|---|---|
onKeyUp | (event: KeyboardEvent, key: K) => void | — | Callback fired on key release. |
preventDefault | boolean | false | When true, calls event.preventDefault() when the key matches. |
Return Values
Returns a React.RefObject<T>. Attach it to any element to scope the listener to that element instead of window.
Key Combos & Modifiers
Combine modifier keys with + syntax:
| Modifier Alias | Description |
|---|---|
Ctrl / Control | Control key |
Shift | Shift key |
Alt / Opt / Option | Alt / Option key |
Meta / Cmd / Command | Command (Mac) or Windows key |
CtrlOrCmd | Ctrl on Windows/Linux, Cmd on macOS |
AltOrCmd | Alt on Windows/Linux, Cmd on macOS |
space | Space bar |