Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useKeyModifier.md
1---2category: Sensors3---45# useKeyModifier67Reactive [Modifier State](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState). Tracks state of any of the [supported modifiers](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState#browser_compatibility) - see Browser Compatibility notes.89<CourseLink href="https://vueschool.io/lessons/alt-drag-to-clone-tasks?friend=vueuse">Learn useKeyModifier with this FREE video lesson from Vue School!</CourseLink>1011## Usage1213```ts14import { useKeyModifier } from '@vueuse/core'1516const capsLockState = useKeyModifier('CapsLock')1718console.log(capsLockState.value)19```2021## Events2223You can customize which events will prompt the state to update. By default, these are `mouseup`, `mousedown`, `keyup`, `keydown`. To customize these events:2425```ts26import { useKeyModifier } from '@vueuse/core'2728const capsLockState = useKeyModifier('CapsLock', { events: ['mouseup', 'mousedown'] })2930console.log(capsLockState) // null3132// Caps Lock turned on with key press33console.log(capsLockState) // null3435// Mouse button clicked36console.log(capsLockState) // true37```3839## Initial State4041By default, the returned ref will be `Ref<null>` until the first event is received. You can explicitly pass the initial state to it via:4243```ts44import { useKeyModifier } from '@vueuse/core'45// ---cut---46const capsLockState1 = useKeyModifier('CapsLock') // Ref<boolean | null>47const capsLockState2 = useKeyModifier('CapsLock', { initial: false }) // Ref<boolean>48```4950## Type Declarations5152```ts53export type KeyModifier =54| "Alt"55| "AltGraph"56| "CapsLock"57| "Control"58| "Fn"59| "FnLock"60| "Meta"61| "NumLock"62| "ScrollLock"63| "Shift"64| "Symbol"65| "SymbolLock"66export interface UseModifierOptions<Initial> extends ConfigurableDocument {67/**68* Event names that will prompt update to modifier states69*70* @default ['mousedown', 'mouseup', 'keydown', 'keyup']71*/72events?: WindowEventName[]73/**74* Initial value of the returned ref75*76* @default null77*/78initial?: Initial79}80export type UseKeyModifierReturn<Initial> = ShallowRef<81Initial extends boolean ? boolean : boolean | null82>83export declare function useKeyModifier<Initial extends boolean | null>(84modifier: KeyModifier,85options?: UseModifierOptions<Initial>,86): UseKeyModifierReturn<Initial>87```88