These composables track which keys the user is currently holding down. They're optional UI helpers for displaying live key indicators or swapping button labels while Shift is held. To reveal shortcut hints when a modifier is held, see Hotkey Hints.
Each composable returns a ref, which templates unwrap for you.
useHeldKeys() returns currently held keys in press order, such as ['Control', 'Shift', 'A']:
<script setup lang="ts">
import { useHeldKeys } from '@tanstack/vue-hotkeys'
const heldKeys = useHeldKeys()
</script>
<template>
<div>
{{ heldKeys.length > 0 ? `Held: ${heldKeys.join(' + ')}` : 'No keys held' }}
</div>
</template>To format held keys consistently (Shift to ⇧ on macOS, Escape to Esc), pass { key } to the function from useFormatForDisplay. Wrapping in { key } accepts raw browser key names outside the strict Key type.
useKeyHold returns whether one key is held. It updates only when that key is pressed or released, and accepts a ref or getter for a key that changes:
<script setup lang="ts">
import { useKeyHold } from '@tanstack/vue-hotkeys'
const props = defineProps<{ file: File }>()
const isShiftHeld = useKeyHold('Shift')
</script>
<template>
<div>
<span>{{ file.name }}</span>
<button v-if="isShiftHeld" @click="permanentlyDelete(file)">Delete permanently</button>
<button v-else @click="moveToTrash(file)">Move to trash</button>
</div>
</template>Letters match case-insensitively ('a' and 'A' are identical), while named keys are case-sensitive ('Shift', 'Escape').
useHeldKeyCodes maps each held key to the physical key that's down, from event.code:
<script setup lang="ts">
import { useHeldKeyCodes } from '@tanstack/vue-hotkeys'
const heldCodes = useHeldKeyCodes() // e.g. { Shift: 'ShiftLeft', Control: 'ControlRight' }
</script>
<template>
<p v-if="heldCodes.Shift === 'ShiftRight'">Right Shift is held</p>
</template>To inspect held keys inside a click or canvas handler without tracking them, read the shared tracker from getKeyStateTracker():
<script setup lang="ts">
import { getKeyStateTracker } from '@tanstack/vue-hotkeys'
const tracker = getKeyStateTracker()
function onClick() {
if (tracker.isKeyHeld('Space')) startPanning()
else if (tracker.isAnyKeyHeld(['Shift', 'Control'])) addToSelection()
else select()
}
</script>
<template>
<canvas @click="onClick" />
</template>Held keys reset automatically when the window loses focus, so a key released after switching apps never stays stuck. See Held keys reset when the window loses focus.
See the useHeldKeys example and the useKeyHold example.