Track Held Keys and Modifier State in Alpine
These functions report which keys the user is holding right now. They're optional helpers for your UI: show which modifiers are down, or change a button while Shift is held. You don't need them to register hotkeys. To show a button's shortcut while the user holds its modifiers, see Hotkey Hints.
Create them on an owner from createHotkeysScope. Each returns an object with a reactive value, so read value where you need it.
Show which keys are held#
createHeldKeys holds the held keys in the order they were pressed, such as ['Control', 'Shift', 'A']:
import Alpine from 'alpinejs'
import { createHotkeysScope } from '@tanstack/alpine-hotkeys'
Alpine.data('keyDisplay', () => {
const hotkeys = createHotkeysScope()
const heldKeys = hotkeys.createHeldKeys()
return {
get label() {
return heldKeys.value.length > 0 ? `Held: ${heldKeys.value.join(' + ')}` : 'No keys held'
},
destroy() {
hotkeys.destroy()
},
}
})<div x-data="keyDisplay" x-text="label"></div>Check whether a key is held#
createKeyHold tracks whether one key is held. It updates only when that key is pressed or released, and accepts a getter for a key that changes:
Alpine.data('fileItem', (file: File) => {
const hotkeys = createHotkeysScope()
const shift = hotkeys.createKeyHold('Shift')
return {
file,
get isShiftHeld() {
return shift.value
},
destroy() {
hotkeys.destroy()
},
}
})<div x-data="fileItem(file)">
<span x-text="file.name"></span>
<button x-show="isShiftHeld" @click="permanentlyDelete(file)">Delete permanently</button>
<button x-show="!isShiftHeld" @click="moveToTrash(file)">Move to trash</button>
</div>Tell left and right modifiers apart#
createHeldKeyCodes maps each held key to the physical key that's down, from event.code:
Alpine.data('modifierSides', () => {
const hotkeys = createHotkeysScope()
const heldCodes = hotkeys.createHeldKeyCodes() // e.g. value: { Shift: 'ShiftLeft', Control: 'ControlRight' }
return {
get rightShift() {
return heldCodes.value.Shift === 'ShiftRight'
},
destroy() {
hotkeys.destroy()
},
}
})<p x-data="modifierSides" x-show="rightShift">Right Shift is held</p>Read held keys in an event handler#
To read the held keys in an event handler without tracking them, use the shared tracker from getKeyStateTracker:
import { getKeyStateTracker } from '@tanstack/alpine-hotkeys'
Alpine.data('canvas', () => {
const tracker = getKeyStateTracker()
return {
onClick() {
if (tracker.isKeyHeld('Space')) startPanning()
else if (tracker.isAnyKeyHeld(['Shift', 'Control'])) addToSelection()
else select()
},
}
})<canvas x-data="canvas" @click="onClick()"></canvas>The tracker starts listening the first time you get it, so get it when the component is created, before the user presses the keys you care about. Call its methods inside the handler to read the state at that moment. It also has getHeldKeys() and areAllKeysHeld(keys). Don't destroy the tracker when a component is destroyed, because every owner shares it.
For a mouse or wheel event, event.ctrlKey, shiftKey, altKey, and metaKey already report the modifiers. Browsers also set ctrlKey for a trackpad pinch, so it doesn't always mean Control is held.
Held keys clear when the window loses focus, so a key released after switching apps doesn't stay held. Held keys reset when the window loses focus covers the cases the tracker handles.
The createHeldKeys example shows held keys and their codes, and the createKeyHold example shows modifier indicators.