<template>
<div></div>
</template>
<script lang="ts" setup>
defineShortcuts({
ctrl_v: {
usingInput: false,
handler: () => {
useSonner("Nice!", { description: "You just used a shortcut!" });
},
},
});
</script>
Shortcuts keys are written as the literal keyboard key value. Combinations are made with _
separator. Chained shortcuts are made with -
separator.
Modifiers are also available:
meta
: acts as Command
for MacOS and Control
for othersctrl
: acts as Control
shift
: acts as Shift
and is only necessary for alphabetic keysExamples of keys:
escape
: will trigger by hitting Esc
meta_k
: will trigger by hitting ⌘
and K
at the same time on MacOS, and Ctrl
and K
on Windows and Linuxctrl_k
: will trigger by hitting Ctrl
and K
at the same time on MacOS, Windows and Linuxshift_e
: will trigger by hitting Shift
and E
at the same time on MacOS, Windows and Linux?
: will trigger by hitting ? on some keyboard layouts, or for example Shift and /, which results in ? on US Mac keyboardsg-d
: will trigger by hitting g
then d
with a maximum delay of 800ms by defaultarrowleft
: will trigger by hitting ←
(also: arrowright
, arrowup
, arrowdown
)usingInput
Prop: usingInput?: string | boolean
By default, usingInput
is false
, meaning it will only trigger when no input is focused. When set to true
, the shortcut will also trigger when any input is focused.
For a more advanced behavior, usingInput
can be set to the name of an input, so it only triggers when focusing this specific input.
In case the shortcut does not need any config, it can be written as a function.
defineShortcuts({
"?": () => openHelpModal(),
});
useShortcuts
To display shortcuts in your app according to the user's OS, you can use the useShortcuts
composable.
<script setup>
const { metaSymbol } = useShortcuts();
</script>
<template>
<UKbd>{{ metaSymbol }}</UKbd>
</template>
metaSymbol
will display either ⌘
on MacOS or Ctrl
on any other OS