Usage
Use the auto-imported defineShortcuts composable to define keyboard shortcuts.
<script setup lang="ts">
const open = ref(false)
defineShortcuts({
meta_k: () => {
open.value = !open.value
}
})
</script>
- Shortcuts are automatically adjusted for non-macOS platforms, converting
metatoctrl. - The composable uses VueUse's
useEventListenerto handle keydown events. - For a complete list of available shortcut keys, refer to the
KeyboardEvent.keyAPI documentation. Note that the key should be written in lowercase.
API
defineShortcuts(config: MaybeRef<ShortcutsConfig>, options?: ShortcutsOptions): () => void
Define keyboard shortcuts for your application. Returns a function that removes the listener, in case you need to stop the shortcuts before the component unmounts.
Parameters
ref to update the shortcuts reactively. A value of false, null or undefined skips that shortcut, which is how you enable one conditionally.800.false(default): Usese.keyfor character-based matching (Layout specific)true: Usese.codefor physical key matching (Layout agnostic)
Shortcut definition
Shortcuts are defined using the following format:
- Single key:
'a','b','1','?', etc. - Key combinations: Use
_to separate keys, e.g.'meta_k','ctrl_shift_f' - Key sequences: Use
-to define a sequence, e.g.'g-d'
Modifiers
meta/command: Represents⌘ Commandon macOS andCtrlon other platformsctrl: RepresentsCtrlon all platformsshift: Used for alphabetic keys when Shift is requiredalt/option: Represents⌥ Optionon macOS andAlton other platforms. Matched by physical key position, since Option rewrites the character on macOS
Special keys
Use these names to match special keys.
escape: Triggers on Esc keyenter: Triggers on Enter keyarrowleft,arrowright,arrowup,arrowdown: Trigger on respective arrow keystab: Triggers on Tab keybackspace: Triggers on Backspace keydelete: Triggers on Delete keyspace: Triggers on the space bar. RequireslayoutIndependentunless combined withalt
Shortcut configuration
Each shortcut can be defined as a function or an object with the following properties:
interface ShortcutConfig { handler: (e?: KeyboardEvent) => void; usingInput?: boolean | string }
Parameters
false(default): Shortcut only triggers when no input is focusedtrue: Shortcut triggers even when any input is focusedstring: Shortcut only triggers when the specified input (by name) is focused
Examples
Basic usage
<script setup lang="ts">
defineShortcuts({
'?': () => openHelpModal(),
'meta_k': () => openCommandPalette(),
'g-d': () => navigateToDashboard()
})
</script>
With input focus handling
Use usingInput to trigger a shortcut only when a specific input is focused.
<template>
<B24Input v-model="query" name="queryInput" />
</template>
<script setup lang="ts">
const query = ref('')
defineShortcuts({
enter: {
usingInput: 'queryInput',
handler: () => performSearch()
},
escape: {
usingInput: true,
handler: () => clearSearch()
}
})
</script>
Extracting shortcuts from menu items
Use the extractShortcuts utility to automatically define shortcuts from menu items.