Kbd
Display a key or a keyboard shortcut. This component only displays keys; use useHotkey to register a shortcut.
Import
import { HuiKbd, useHotkey, formatHotkey, parseHotkey, matchHotkey } from '@nscode/handy-ui'
import type { KbdProps, UseHotkeyOptions, ParsedHotkey } from '@nscode/handy-ui'
Examples
Single keys
Without keys, the default slot is rendered in a native <kbd> element.
<script setup lang="ts">
import { HuiKbd, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="center">
<HuiKbd>Esc</HuiKbd>
<HuiKbd>Enter</HuiKbd>
<HuiKbd>↑</HuiKbd>
<HuiKbd>↓</HuiKbd>
</HuiSpace>
</template>
Shortcuts and platforms
mod is displayed as ⌘ on Mac, iPhone and iPad, or Ctrl on other platforms. Other modifiers also use platform-specific names or symbols.
<script setup lang="ts">
import { HuiKbd, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center"><span>mod+k</span><HuiKbd keys="mod+k" /></HuiSpace>
<HuiSpace align="center"><span>mod+shift+p</span><HuiKbd keys="mod+shift+p" /></HuiSpace>
<HuiSpace align="center"><span>alt+up</span><HuiKbd keys="alt+up" /></HuiSpace>
<HuiSpace align="center"><span>ctrl++</span><HuiKbd keys="ctrl++" /></HuiSpace>
</HuiSpace>
</template>
Sizes
Available sizes are large, default and small. When omitted, the nearest HuiConfigProvider supplies the size.
<script setup lang="ts">
import { HuiConfigProvider, HuiKbd, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center"><span>large</span><HuiKbd keys="mod+k" size="large" /></HuiSpace>
<HuiSpace align="center"><span>default</span><HuiKbd keys="mod+k" size="default" /></HuiSpace>
<HuiSpace align="center"><span>small</span><HuiKbd keys="mod+k" size="small" /></HuiSpace>
<HuiConfigProvider size="small">
<HuiSpace align="center"><span>Global small</span><HuiKbd keys="mod+k" /></HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Separators
The default separator is +. An empty string removes separator elements, which is useful for the compact Mac notation.
<script setup lang="ts">
import { HuiKbd, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center"><span>Default +</span><HuiKbd keys="mod+shift+k" /></HuiSpace>
<HuiSpace align="center"><span>Empty</span><HuiKbd keys="mod+shift+k" separator="" /></HuiSpace>
<HuiSpace align="center"
><span>Custom ·</span><HuiKbd keys="mod+shift+k" separator=" · "
/></HuiSpace>
</HuiSpace>
</template>
Shortcuts in tooltips
<script setup lang="ts">
import { HuiButton, HuiKbd, HuiMessage, HuiSpace, HuiTooltip } from '@nscode/handy-ui'
const save = () => HuiMessage.success('Saved')
</script>
<template>
<HuiTooltip>
<template #content>
<HuiSpace align="center"><span>Save</span><HuiKbd keys="mod+s" /></HuiSpace>
</template>
<HuiButton @click="save">Save</HuiButton>
</HuiTooltip>
</template>
Registering and enabling shortcuts
useHotkey cleans up when the component unmounts. With the default ignoreInputs: 'auto', mod+s also works inside inputs, while / remains available for typing. Click a non-input area before pressing / to focus the search field.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInput, HuiKbd, HuiMessage, HuiSpace, HuiSwitch, useHotkey } from '@nscode/handy-ui'
const enabled = ref(true)
const text = ref('')
const saved = ref(0)
const search = ref<InstanceType<typeof HuiInput>>()
useHotkey(
'mod+s',
() => {
saved.value++
HuiMessage.success('Saved')
},
{ enabled }
)
useHotkey('/', () => search.value?.focus(), { enabled })
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="center"><HuiSwitch v-model="enabled" /><span>Enable shortcuts</span></HuiSpace>
<HuiSpace align="center"
><HuiKbd keys="mod+s" /><span>Save; triggered {{ saved }} times</span></HuiSpace
>
<HuiSpace align="center"><HuiKbd keys="/" /><span>Focus the search field</span></HuiSpace>
<HuiInput
ref="search"
v-model="text"
placeholder="Typing / here does not trigger the shortcut"
/>
</HuiSpace>
</template>
Targets, keyup and early cleanup
Listen to a specific area instead of the document. An explicit empty target registers nothing; the listener attaches when the element mounts. Calling the returned function permanently unregisters the shortcut, including future target changes. In this example, releasing Space inside the input also increments the count while preserving normal typing.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiSpace, useHotkey } from '@nscode/handy-ui'
const area = ref<HTMLElement | null>(null)
const count = ref(0)
const stopped = ref(false)
const text = ref('')
const stop = useHotkey('space', () => count.value++, {
target: area,
event: 'keyup',
ignoreInputs: false,
preventDefault: false
})
const cancel = () => {
stop()
stopped.value = true
}
</script>
<template>
<div ref="area" tabindex="0" style="padding: 12px; border: 1px solid var(--hui-border-color)">
<HuiSpace direction="vertical" fill>
<span>Release Space here: {{ count }} times; {{ stopped ? 'Stopped' : 'Listening' }}</span>
<HuiInput v-model="text" placeholder="Spaces still type normally and also update the count" />
<HuiButton :disabled="stopped" @click="cancel">Stop listening</HuiButton>
</HuiSpace>
</div>
</template>
Local Escape inside a modal
Escape preserves the default action unless configured otherwise, so a shortcut handler and the surrounding modal can both respond. To cancel local editing while keeping the modal open, use a local target and explicitly pass preventDefault: true. Editable areas also require ignoreInputs: false, since auto ignores plain Escape. Disable the local shortcut after cancelling editing so the next Escape closes the modal.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiModal, HuiSpace, useHotkey } from '@nscode/handy-ui'
const open = ref(false)
const editing = ref(true)
const editor = ref<HTMLElement | null>(null)
const text = ref('Press Escape to cancel editing, then again to close the modal')
const cancelled = ref(0)
useHotkey(
'esc',
() => {
editing.value = false
cancelled.value++
},
{
target: editor,
enabled: editing,
ignoreInputs: false,
preventDefault: true
}
)
const show = () => {
editing.value = true
open.value = true
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiButton @click="show">Open local editor</HuiButton>
<span>Editing cancelled {{ cancelled }} times</span>
<HuiModal v-model:visible="open" title="Local Escape takes priority">
<div ref="editor">
<label>Content<textarea v-model="text" :readonly="!editing" /></label>
<p>
{{
editing
? 'Editing: press Escape in the text area to cancel'
: 'Editing cancelled: press Escape again to close'
}}
</p>
</div>
</HuiModal>
</HuiSpace>
</template>
Use keydown so the local listener marks defaultPrevented before the event bubbles to the overlay’s close listener. keyup occurs after the overlay has handled the key press and cannot prevent that close. Escape outside the local target still reaches the modal.
Formatting keys
formatHotkey returns an array of strings. Pass an explicit platform flag to show instructions for another platform. It does not register any listeners.
<script setup lang="ts">
import { HuiKbd, HuiSpace, formatHotkey } from '@nscode/handy-ui'
const mac = formatHotkey('mod+shift+k', true)
const other = formatHotkey('mod+shift+k', false)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center"
><span>Mac</span><HuiKbd v-for="key in mac" :key="key">{{ key }}</HuiKbd></HuiSpace
>
<HuiSpace align="center"
><span>Other platforms</span
><HuiKbd v-for="key in other" :key="key">{{ key }}</HuiKbd></HuiSpace
>
</HuiSpace>
</template>
Localized screen reader names
In keys mode, ⌘, ⇧, ⌥, ⌃, ↵, ⌫, arrow keys, Ctrl, Shift, Esc and Tab receive full screen reader names. Names follow the language of HuiConfigProvider, while keycaps retain platform symbols. Separators are hidden from screen readers; letters and digits keep their visible text. Switch languages below and use a screen reader to check the names.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiConfigProvider,
HuiKbd,
HuiSpace,
HuiSwitch,
localeEn,
localeZhCn
} from '@nscode/handy-ui'
const english = ref(false)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center">
<HuiSwitch v-model="english" />
<span>{{ english ? 'English screen reader names' : 'Chinese screen reader names' }}</span>
</HuiSpace>
<HuiConfigProvider :locale="english ? localeEn : localeZhCn">
<HuiSpace wrap align="center">
<HuiKbd keys="mod+shift+enter" />
<HuiKbd keys="alt+backspace" />
<HuiKbd keys="esc" />
<HuiKbd keys="tab" />
<HuiKbd keys="up" />
<HuiKbd keys="down" />
<HuiKbd keys="ctrl+left" />
<HuiKbd keys="right" />
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Without keys, default slot content is preserved and no screen reader names are generated. Supply accessible descriptions for custom symbols in your application.
Props
| Prop | Description | Type | Default |
|---|---|---|---|
| keys | Shortcut expression; omit to display the default slot | string | — |
| separator | Separator between keys; empty string removes the elements | string | '+' |
| size | Size; inherits from ConfigProvider when omitted | 'large' | 'default' | 'small' | 'default' |
Events
None. HuiKbd only displays keys; use useHotkey for keyboard actions.
Slots
| Slot | Description | Parameters |
|---|---|---|
| default | Single-key content, used when keys is omitted | — |
Methods
No component instance methods.
Utilities
useHotkey
useHotkey(
keys: MaybeRefOrGetter<string | string[]>,
handler: (event: KeyboardEvent) => void,
options?: UseHotkeyOptions
): () => void
Keys, target and enabled state may be reactive. Use an array for multiple shortcuts. The returned function unregisters the listener early; the active Vue scope also cleans up automatically when disposed.
| Option | Description | Type | Default |
|---|---|---|---|
| target | Listener target; an explicit empty target registers nothing | MaybeRefOrGetter<EventTarget | null | undefined> | document |
| enabled | Whether the shortcut is active | MaybeRefOrGetter<boolean> | true |
| ignoreInputs | Ignore editable areas; auto permits only Ctrl, ⌘ and Alt combinations | boolean | 'auto' | 'auto' |
| preventDefault | Prevent the default action on a match; explicit true/false takes priority | boolean | false for Escape, true for other keys |
| event | Listen to key presses or releases | 'keydown' | 'keyup' | 'keydown' |
Editable areas include input, textarea, select and editable content. Events during IME composition are ignored. Modifiers must match exactly. Symbol keys such as ? ignore Shift to accommodate keyboard layouts.
formatHotkey, parseHotkey and matchHotkey
formatHotkey(hotkey: string, mac?: boolean): string[]
parseHotkey(hotkey: string, mac?: boolean): ParsedHotkey
matchHotkey(event: KeyboardEvent, hotkey: ParsedHotkey): boolean
mac defaults to the current platform. Aliases include esc, return, del, space, up, down, left and right. cmd / command mean Meta; option means Alt. Write a plus key as plus or ctrl++. Expressions containing only modifiers do not trigger a shortcut.
Types
import type { ComponentSize } from '@nscode/utils'
import type { MaybeRefOrGetter } from 'vue'
interface KbdProps {
keys?: string
separator?: string
size?: ComponentSize
}
interface UseHotkeyOptions {
target?: MaybeRefOrGetter<EventTarget | null | undefined>
enabled?: MaybeRefOrGetter<boolean>
ignoreInputs?: boolean | 'auto'
preventDefault?: boolean
event?: 'keydown' | 'keyup'
}
interface ParsedHotkey {
key: string
ctrl: boolean
meta: boolean
alt: boolean
shift: boolean
}