packages/core/useKeyModifier/index.md
Reactive Modifier State. Tracks state of any of the supported modifiers - see Browser Compatibility notes.
<CourseLink href="https://vueschool.io/lessons/alt-drag-to-clone-tasks?friend=vueuse">Learn useKeyModifier with this FREE video lesson from Vue School!</CourseLink>
import { useKeyModifier } from '@vueuse/core'
const capsLockState = useKeyModifier('CapsLock')
console.log(capsLockState.value)
You can customize which events will prompt the state to update. By default, these are mouseup, mousedown, keyup, keydown. To customize these events:
import { useKeyModifier } from '@vueuse/core'
const capsLockState = useKeyModifier('CapsLock', { events: ['mouseup', 'mousedown'] })
console.log(capsLockState) // null
// Caps Lock turned on with key press
console.log(capsLockState) // null
// Mouse button clicked
console.log(capsLockState) // true
By default, the returned ref will be Ref<null> until the first event is received. You can explicitly pass the initial state to it via:
import { useKeyModifier } from '@vueuse/core'
// ---cut---
const capsLockState1 = useKeyModifier('CapsLock') // Ref<boolean | null>
const capsLockState2 = useKeyModifier('CapsLock', { initial: false }) // Ref<boolean>