BlogPlaygroundOne

Hotkeys

Provides a simple and powerful way to register keyboard shortcuts that work across different platforms and input contexts.


Quick start

To get started, import the useHotkey composable:

path/to/Component.vue
<script setup>
  import { useHotkey } from 'vuetify'

  useHotkey('ctrl+s', () => {
    console.log('Save action')
  })
</script>

Usage

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.

Activity log
No Events
Current hotkey:
<span>Current hotkey:</span> <v-hotkey keys="cmd+b" /> <pre>log: {{ log }}</pre>
<script setup> import { shallowRef } from 'vue' import { useHotkey } from 'vuetify' const log = shallowRef('') function onHotkey () { log.value += '\n- Hotkey pressed' } useHotkey('cmd+b', onHotkey, { sequenceTimeout: 2000, }) </script>

API

ComposableDescription
useHotkeyThe useHotkey composable

Guide

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.

Basic hotkeys

Register simple keyboard shortcuts by passing a key combination string and callback function:

Press the keyboard shortcuts below to trigger actions:

HotkeyActionDescription
Save DocumentCross-platform save shortcut
Undo ActionStandard undo shortcut

Activity Log:

No hotkeys triggered yet...

Key sequences

Create multi-step keyboard shortcuts by separating keys with dashes. Users must press keys in sequence within the timeout period:

Key sequences require pressing keys in order within the timeout period. Try the sequences below:

HotkeyActionDescription
Command PalettePress Ctrl+X, then P (within timeout)
Go to TopPress G, then G (Vim-style navigation)
Save FilePress Ctrl+X, then Ctrl+S (Emacs-style)

Activity Log:

No sequences triggered yet...

Platform awareness

The composable automatically handles platform differences. Use cmd for cross-platform compatibility or specific modifiers for platform-specific behavior:

Try the hotkeys below to see how they adapt to your platform:

Available Hotkeys

HotkeyActionPlatform Behavior
CopyCtrl on PC
PasteCtrl on PC
RedoCtrl+Shift on PC
Select All (Explicit Ctrl)Always Ctrl, even on Mac
Find in PageCtrl key on PC

Activity Log:

No platform-aware hotkeys triggered yet...

Platform Detection Details

PropertyValue
Platform DetectedWindows/Linux
User AgentMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
cmd maps toControl key (Ctrl)

Reactive hotkeys

Key combinations can be reactive, allowing you to change hotkeys dynamically:

Hotkey combinations can be reactive, allowing you to change them dynamically. Try changing the hotkey combinations below:

Current Active Hotkeys

HotkeyActionStatus
Save Document
Active
Undo Action
Active
Custom Action
Active

Activity Log:

No reactive hotkeys triggered yet...

Options

Customize hotkey behavior with options for event type, input handling, and more:

Customize hotkey behavior with various options. Change the settings below and test the hotkeys:

Options Configuration

Test Hotkeys

HotkeyActionSettings
Test Basic HotkeyEvent: keydown
Test SequenceTimeout: 2000ms
Test in InputEnabled in inputs

Current Configuration

OptionValueDescription
event
keydown
When to trigger the hotkey callback
inputs
true
Allow hotkeys when input elements are focused
preventDefault
true
Prevent default browser behavior
sequenceTimeout
2000ms
Time limit for completing key sequences

Activity Log:

No hotkeys with options triggered yet...

Configuration 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
}

Event type

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 triggers

Input handling

By 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:

  • Keep inputs: false (default) for global shortcuts like save/copy/paste
  • Use inputs: true for application-specific shortcuts that should work everywhere
  • Consider using different key combinations for input-specific actions

Prevent default behavior

Control 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:

  • When you want to enhance rather than replace browser behavior
  • For accessibility keys that should maintain their original function
  • When testing or debugging hotkey interactions

Sequence timeout

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 })

Key combination syntax

The hotkey string supports various modifiers and special keys:

Modifiers

  • cmd / meta - Command key (Mac) / Control key (PC) - recommended over ctrl for cross-platform
  • ctrl - Control key (all platforms)
  • alt - Alt key (all platforms)
  • shift - Shift key (all platforms)

Special keys

  • enter, escape, tab, space, backspace, delete
  • arrowup, arrowdown, arrowleft, arrowright
  • home, end, pageup, pagedown
  • f1 through f12
  • plus, slash, underscore, and minus/hyphen for literal +, /, _, and -

Syntax rules

  • Parsing is performed left to right, so ctrl+s-a will produce “ctrl+s and then a
  • Use + or _ to combine modifiers with keys: ctrl+s, ctrl_s
  • Use / to separate alternative keys or combinations: ctrl+k/ctrl+f, up/down
  • Use - to create sequences: ctrl+k-p (Ctrl+K, then P) ctrl+k/ctrl+f-p (Ctrl+K or Ctrl+F, then P)
  • Keys are case-insensitive: Ctrl+S equals ctrl+s

Check out all possible keycodes.

Manual cleanup

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()

Avoiding key binding conflicts

Prevent conflicts by avoiding reserved shortcuts and organizing hotkeys systematically:

Common conflicts to avoid

// ❌ 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)

Best practices

  • Test cross-platform: Verify shortcuts work on Windows, macOS, and Linux!
  • Use modifier combinations: Prefer Ctrl+Shift+Key for custom actions
  • Document your shortcuts: Maintain a list of all application hotkeys in your application

Ready for more?

Continue your learning with related content selected by the Team or move between pages by using the navigation links below.