useHotkeys
The useHotkeys hook lets you listen to keyboard shortcuts declaratively and run a callback when the user presses the specified key combination.
Every code example on this page is editable — change the code and see the result immediately.
Import
import { useHotkeys } from 'react-hotkeys-hook'
Or with require:
const { useHotkeys } = require('react-hotkeys-hook')
// or
const useHotkeys = require('react-hotkeys-hook').useHotkeys
Simple hotkey
The simplest use case is binding a single key to a callback:
function ExampleComponent() { useHotkeys('a', () => alert('Key a was pressed')) return <span>Press the a key to see it work.</span> }
Click inside the live preview area to focus it before pressing keys.
Inside the callback you can do anything. A common pattern is updating component state:
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('b', () => setCount((prevCount) => prevCount + 1)) return <span>Pressed the 'b' key {count} times.</span> }
useHotkeys attaches the listener when the component mounts and removes it when the component unmounts.
Multiple hotkeys per component
Since useHotkeys is a hook, you can call it multiple times in the same component:
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('q', () => setCount((prevCount) => prevCount + 1)) useHotkeys('w', () => setCount((prevCount) => prevCount - 1)) return <span>The count is {count}.</span> }
Key combinations
You can listen to key combinations — multiple keys pressed together. For example, a Jira-like Shift+C shortcut to create a new ticket:
function CreateIssue() { const [showIssueCreatorModal, setShowIssueCreatorModal] = useState(false) useHotkeys('shift+c', () => setShowIssueCreatorModal(true)) return ( <> {showIssueCreatorModal && <div>MODAL CONTENT</div>} {!showIssueCreatorModal && <div>issue list</div>} </> ) }
You are not limited to two keys. Combinations like ctrl+shift+a+c are also valid (though perhaps not the most user-friendly).
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('ctrl+shift+a+c', () => setCount((prevCount) => prevCount + 1)) return <span>Pressed the 'ctrl+shift+a+c' key {count} times.</span> }
It does not matter whether you write CTRL+S, Ctrl+s, ctrl+S, or any other case variation. They all listen for the ctrl and s keys. Uppercase letters are treated as lowercase — to listen for an uppercase S, use shift+s.
Multiple bindings for one callback
You can bind several different key combinations to the same callback by separating them with a comma:
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('ctrl+shift+a+c, c, shift+c', () => setCount((prevCount) => prevCount + 1)) return <span>Received the combination {count} times.</span> }
You can also pass an array as the first argument: useHotkeys(['ctrl+shift+a+c', 'c', 'shift+c'], ...)
The ctrl+shift+a+c and shift+c combinations are used by multiple examples on this page. If you already pressed one of them, the example above may start with a count greater than zero. Hotkeys are attached globally unless you use the returned ref to scope them.
Sequential hotkeys
You can listen for keys pressed in sequence using the > character:
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('g>h>i', () => setCount((prevCount) => prevCount + 1)) return <span>Received the combination {count} times.</span> }
In this example, the user must press g, then h, then i to trigger the callback.
Timeout
By default, useHotkeys allows 1 second between each key in a sequence. If the user does not press the next key within that window, the sequence resets. You can change this with the sequenceTimeoutMs option:
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('g>h>i>j', () => setCount((prevCount) => prevCount + 1), { sequenceTimeoutMs: 10000, }) return <span>Received the combination {count} times.</span> }
Here the user has 10 seconds between each key to complete the g>h>i>j sequence (up to 30 seconds total for all four keys).
Modifiers
useHotkeys supports the following modifier keys:
shiftaltctrlmetamod— requiresmeta(cmd) on macOS andctrlon other platforms.
Use mod for shortcuts that follow the platform's primary modifier. To require a specific modifier on every platform, use ctrl or meta directly. For example, use ctrl to listen for Control on macOS.
Cross-platform compatibility
Since version 4, alt and option are treated as identical. The meta key is cmd on macOS and the Windows/os key on Windows/Linux.
function ExampleComponent() { const [count, setCount] = useState(0) useHotkeys('ctrl+shift+a+c, c, shift+c, alt+n, ctrl+d, meta+d', () => setCount((prevCount) => prevCount + 1)) return <span>Received the combination {count} times.</span> }
Special keys
Arrow keys, return, space, and other special keys have dedicated names:
backspacetabclearenterorreturnescorescapespaceup,down,left,rightpageup,pagedowndelordeletef1,f2...f19
Some shortcuts cannot be overridden because they are essential to the browser's safe browsing experience. In Chrome, for example:
meta+w— close tabmeta+n— new windowmeta+t— new tabmeta+shift+w— close all tabs of the current windowmeta+shift+n— open incognito windowmeta+shift+t— reopen last closed tabmeta+1..9— focus corresponding tab