Appearance
useKeyboardShortcut
Registers a global keyboard shortcut listener.
Signature
ts
function useKeyboardShortcut(
shortcut: KeyboardShortcut,
handler: (event: KeyboardEvent) => void,
options?: KeyboardShortcutOptions
): voidParameters
KeyboardShortcut
| Property | Type | Default | Description |
|---|---|---|---|
key | string | Required | Key to listen for (case-insensitive) |
ctrl | boolean | false | Require Ctrl key |
cmd | boolean | false | Require Cmd key (treated same as ctrl) |
shift | boolean | false | Require Shift key |
alt | boolean | false | Require Alt key |
handler
Callback function invoked when the shortcut is triggered.
KeyboardShortcutOptions
| Property | Type | Default | Description |
|---|---|---|---|
enabled | boolean | true | Whether shortcut is active |
preventDefault | boolean | true | Prevent default browser behavior |
ignoreInputs | boolean | true | Ignore when focused on input elements |
Example
tsx
useKeyboardShortcut(
{ key: 'k', ctrl: true },
() => setIsOpen(true),
{ enabled: !isOpen }
)useKeyboardShortcuts
Registers multiple keyboard shortcuts at once.
Signature
ts
function useKeyboardShortcuts(
shortcuts: ShortcutConfig[],
globalOptions?: KeyboardShortcutOptions
): voidShortcutConfig
ts
interface ShortcutConfig {
shortcut: KeyboardShortcut
handler: (event: KeyboardEvent) => void
options?: KeyboardShortcutOptions
}Example
tsx
useKeyboardShortcuts([
{ shortcut: { key: 'k', ctrl: true }, handler: () => openSearch() },
{ shortcut: { key: 'n', ctrl: true }, handler: () => createNew() },
{ shortcut: { key: 'Escape' }, handler: () => closeModal() },
])