npm i react-hotkeys-hook
A React hook for using keyboard shortcuts in components in a declarative way.
Pressed {count} times.
) } ``` ### Scopes Scopes allow you to group hotkeys together. You can use scopes to prevent hotkeys from colliding with each other. ```jsx harmony const App = () => { return (Pressed {count} times.
) } ``` #### Changing a scope's active state You can change the active state of a scope using the `deactivateScope`, `activateScope` and `toggleScope` functions returned by the `useHotkeysContext()` hook. Note that you have to have your app wrapped in a `Pressed {count} times.
) } ``` ## Documentation & Live Examples * [Quick Start](https://react-hotkeys-hook.vercel.app/docs/intro) * [Documentation](https://react-hotkeys-hook.vercel.app/docs/documentation/installation) * [API](https://react-hotkeys-hook.vercel.app/docs/api/use-hotkeys) ## API ### useHotkeys(keys, callback) ```typescript useHotkeys(keys: string | string[], callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options: Options = {}, deps: DependencyList = []) ``` | Parameter | Type | Required? | Default value | Description | |---------------|---------------------------------------------------------|-----------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `keys` | `string` or `string[]` | required | - | set the hotkeys you want the hook to listen to. You can use single or multiple keys, modifier combinations, etc. This will either be a string or an array of strings. To separate multiple keys, use a colon. This split key value can be overridden with the `splitKey` option. | | `callback` | `(event: KeyboardEvent, handler: HotkeysEvent) => void` | required | - | This is the callback function that will be called when the hotkey is pressed. The callback will receive the browsers native `KeyboardEvent` and the libraries `HotkeysEvent`. | | `options` | `Options` | optional | `{}` | Object to modify the behavior of the hook. Default options are given below. | | `dependencies` | `DependencyList` | optional | `[]` | The given callback will always be memoised inside the hook. So if you reference any outside variables, you need to set them here for the callback to get updated (Much like `useCallback` works in React). | ### Options All options are optional and have a default value which you can override to change the behavior of the hook. | Option | Type | Default value | Description | |--------------------------|--------------------------------------------------------------------------------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `enabled` | `boolean` or `(keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean` | `true` | This option determines whether the hotkey is active or not. It can take a boolean (for example a flag from a state outside) or a function which gets executed once the hotkey is pressed. If the function returns `false` the hotkey won't get executed and all browser events are prevented. | | `enableOnFormTags` | `boolean` or `FormTags[]` | `false` | By default hotkeys are not registered if a focus focuses on an input field. This will prevent accidental triggering of hotkeys when the user is typing. If you want to enable hotkeys, use this option. Setting it to true will enable on all form tags, otherwise you can give an array of form tags to enable the hotkey on (possible options are: `['input', 'textarea', 'select']`) | | `enableOnContentEditable` | `boolean` | `false` | Set this option to enable hotkeys on tags that have set the `contentEditable` prop to `true` | | `combinationKey` | `string` | `+` | Character to indicate keystrokes like `shift+c`. You might want to change this if you want to listen to the `+` character like `ctrl-+`. | | `splitKey` | `string` | `,` | Character to separate different keystrokes like `ctrl+a, ctrl+b`. | | `scopes` | `string` or `string[]` | `*` | With scopes you can group hotkeys together. The default scope is the wildcard `*` which matches all hotkeys. Use the `