packages/docs/src/pages/en/features/hotkey.md
Provides a simple and powerful way to register keyboard shortcuts that work across different platforms and input contexts.
<PageFeatures /> <PromotedEntry />To get started, import the useHotkey composable:
<script setup>
import { useHotkey } from 'vuetify'
useHotkey('ctrl+s', () => {
console.log('Save action')
})
</script>
The hotkey composable takes a key combination string and a callback function. It automatically handles platform differences, key sequences, and provides options for customizing behavior.
<ExamplesUsage name="hotkey" />| Composable | Description |
|---|---|
| useHotkey | The useHotkey composable |
The useHotkey composable provides a declarative way to handle keyboard shortcuts in your Vue applications. It automatically cleans up event listeners when components are unmounted and supports reactive key combinations.
Register simple keyboard shortcuts by passing a key combination string and callback function:
<ExamplesExample file="hotkey/basic" />Create multi-step keyboard shortcuts by separating keys with dashes. Users must press keys in sequence within the timeout period:
<ExamplesExample file="hotkey/sequences" />The composable automatically handles platform differences. Use cmd for cross-platform compatibility or specific modifiers for platform-specific behavior:
Key combinations can be reactive, allowing you to change hotkeys dynamically:
<ExamplesExample file="hotkey/reactive" />Customize hotkey behavior with options for event type, input handling, and more:
<ExamplesExample file="hotkey/options" />The useHotkey composable accepts an optional third parameter of type HotkeyOptions to customize its behavior:
interface HotkeyOptions {
event?: 'keydown' | 'keyup'
inputs?: boolean
preventDefault?: boolean
sequenceTimeout?: number
}
Control which keyboard event triggers the hotkey:
// Trigger on key press (default)
useHotkey('ctrl+s', handleSave, { event: 'keydown' })
// Trigger on key release
useHotkey('ctrl+s', handleSave, { event: 'keyup' })
When to use:
keydown: For actions that should trigger immediately when pressed (most common)keyup: For actions that should wait until the key is released, useful for preventing repeated triggersBy default, hotkeys are disabled when input elements are focused to prevent conflicts with typing:
// Default: hotkeys disabled in input fields
useHotkey('ctrl+s', handleSave)
// Allow hotkeys even when inputs are focused
useHotkey('ctrl+s', handleSave, { inputs: true })
Best practices:
inputs: false (default) for global shortcuts like save/copy/pasteinputs: true for application-specific shortcuts that should work everywhereControl whether the browser's default behavior for the key combination is prevented:
// Prevent browser default (recommended for most cases)
useHotkey('ctrl+s', handleSave, { preventDefault: true })
// Allow browser default behavior
useHotkey('f5', handleRefresh, { preventDefault: false })
When to disable:
For key sequences, control how long users have between keystrokes:
// Default: 1 second between sequence steps
useHotkey('ctrl+k-p', openPalette)
// Faster timeout for expert users
useHotkey('ctrl+k-p', openPalette, { sequenceTimeout: 500 })
// Longer timeout for accessibility
useHotkey('ctrl+k-p', openPalette, { sequenceTimeout: 2000 })
The hotkey string supports various modifiers and special keys:
ctrl+s-a will produce "ctrl+s and then a"+ or _ to combine modifiers with keys: ctrl+s, ctrl_s/ to separate alternative keys or combinations: ctrl+k/ctrl+f, up/down- to create sequences: ctrl+k-p (Ctrl+K, then P) ctrl+k/ctrl+f-p (Ctrl+K or Ctrl+F, then P)Ctrl+S equals ctrl+sCheck out all possible keycodes.
The composable automatically manages cleanup during the Vue component's unmount lifecycle event. If you need to perform manual cleanup, you can save the cleanup function returned by the composable. Executing this function will remove the hotkey listener.
const cleanup = useHotkey('ctrl+s', () => {
console.log('Save action')
})
// Later, manually cleanup
cleanup()
Prevent conflicts by avoiding reserved shortcuts and organizing hotkeys systematically:
// ❌ Browser shortcuts
useHotkey('f5', handleAction) // Refresh
useHotkey('ctrl+t', handleAction) // New tab
useHotkey('ctrl+w', handleAction) // Close tab
// ❌ OS shortcuts
useHotkey('alt+tab', handleAction) // Window switching
// ✅ Safe alternatives
useHotkey('cmd+shift+r', handleAction)
useHotkey('cmd+k-t', handleAction)
useHotkey('alt+1', handleAction)
::: info Hotkeys are a powerful feature, but they utilize APIs that are managed by the browser and the operating system. The reliability of the key bindings you make are subject to factors that can be difficult to control. It's recommended you test your key bindings in the browsers and operating systems you are targeting. :::
Ctrl+Shift+Key for custom actions