Overview
HotkeysProvider is a React context provider that enables advanced hotkey management features like scoping, dynamic scope toggling, and access to all registered hotkeys in your application. It wraps your application (or part of it) and provides context to all child components.
The
HotkeysProvider is optional. You can use useHotkeys without it, but you won’t have access to scope management features or be able to retrieve the list of registered hotkeys.Signature
Props
An array of scope names that should be active when the provider mounts. By default, the wildcard scope
'*' is active, which means all hotkeys without an explicit scope will be active.You can initialize with multiple scopes or specific scopes to control which hotkeys are active on mount.The React components that should have access to the hotkeys context. All
useHotkeys hooks within these components will be able to use scopes and will be registered in the global hotkeys list.Context API
TheHotkeysProvider exposes the following context values via the useHotkeysContext hook:
A read-only array of all currently registered hotkeys in the application. Each hotkey object contains information about the key combination, scopes, description, and metadata.
An array of currently active scope names. Hotkeys are only triggered if they belong to one of the active scopes or the wildcard scope
'*'.Toggle a scope on or off. If the scope is currently active, it will be disabled. If it’s inactive, it will be enabled. When toggling from the wildcard scope
'*', it will replace it with the specified scope.Enable a specific scope, adding it to the list of active scopes. If the wildcard scope
'*' is currently active, it will be replaced with the specified scope.Disable a specific scope, removing it from the list of active scopes. Hotkeys in this scope will no longer trigger.
Usage
Basic Setup
Wrap your application with theHotkeysProvider:
Custom Initial Scopes
Start with specific scopes active instead of the default wildcard:Managing Scopes
Use the context API to dynamically control which hotkeys are active:Accessing Registered Hotkeys
Retrieve all registered hotkeys to display help documentation or keyboard shortcuts:Multi-Modal Application
Create an application with different modes that have different hotkeys:Scope Management with Multiple Components
Coordinate hotkeys across different parts of your application:Important Notes
The wildcard scope
'*' is special. When it’s active, all hotkeys without an explicit scope are active. When you enable a specific scope while '*' is active, the wildcard scope is automatically replaced with your specified scope.Scopes are case-sensitive.
'Editor' and 'editor' are treated as different scopes.Type Definitions
TheHotkey type returned in the hotkeys array has the following structure:
HotkeysContextType provided by the context:
Accessing the Context
To access the context values, use theuseHotkeysContext hook:
Related
- useHotkeys - The main hook for registering hotkeys
- Scopes Concept - Learn more about using scopes effectively
