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/onKeyStroke.md
1---2category: Sensors3---45# onKeyStroke67Listen for keyboard keystrokes. By default, listens on `keydown` events on `window`.89## Usage1011```ts12import { onKeyStroke } from '@vueuse/core'1314onKeyStroke('ArrowDown', (e) => {15e.preventDefault()16})17```1819See [this table](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) for all key codes.2021### Return Value2223Returns a stop function to remove the event listener.2425```ts26const stop = onKeyStroke('Escape', handler)2728// Later, stop listening29stop()30```3132### Listen To Multiple Keys3334```ts35import { onKeyStroke } from '@vueuse/core'3637onKeyStroke(['s', 'S', 'ArrowDown'], (e) => {38e.preventDefault()39})4041// listen to all keys by passing `true` or skipping the key parameter42onKeyStroke(true, (e) => {43e.preventDefault()44})45onKeyStroke((e) => {46e.preventDefault()47})48```4950### Custom Key Predicate5152You can pass a custom function to determine which keys should trigger the handler.5354```ts55import { onKeyStroke } from '@vueuse/core'5657onKeyStroke(58e => e.key === 'A' && e.shiftKey,59(e) => {60console.log('Shift+A pressed')61},62)63```6465### Custom Event Target6667```ts68import { onKeyStroke } from '@vueuse/core'6970onKeyStroke('A', (e) => {71console.log('Key A pressed on document')72}, { target: document })73```7475### Ignore Repeated Events7677The callback will trigger only once when pressing `A` and **holding down**. The `dedupe` option can also be a reactive ref.7879```ts80import { onKeyStroke } from '@vueuse/core'8182onKeyStroke('A', (e) => {83console.log('Key A pressed')84}, { dedupe: true })85```8687Reference: [KeyboardEvent.repeat](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat)8889### Passive Mode9091Set `passive: true` to use a passive event listener.9293```ts94import { onKeyStroke } from '@vueuse/core'9596onKeyStroke('A', handler, { passive: true })97```9899## Directive Usage100101```vue102<script setup lang="ts">103import { vOnKeyStroke } from '@vueuse/components'104105function onUpdate(e: KeyboardEvent) {106// impl...107}108</script>109110<template>111<input v-on-key-stroke:c,v="onUpdate" type="text">112<!-- with options -->113<input v-on-key-stroke:c,v="[onUpdate, { eventName: 'keyup' }]" type="text">114</template>115```116117### Custom Keyboard Event118119```ts120import { onKeyStroke } from '@vueuse/core'121// ---cut---122onKeyStroke('Shift', (e) => {123console.log('Shift key up')124}, { eventName: 'keyup' })125```126127Or128129```ts130import { onKeyUp } from '@vueuse/core'131// ---cut---132onKeyUp('Shift', () => console.log('Shift key up'))133```134135## Shorthands136137- `onKeyDown` - alias for `onKeyStroke(key, handler, {eventName: 'keydown'})`138- `onKeyPressed` - alias for `onKeyStroke(key, handler, {eventName: 'keypress'})`139- `onKeyUp` - alias for `onKeyStroke(key, handler, {eventName: 'keyup'})`140141## Type Declarations142143```ts144export type KeyPredicate = (event: KeyboardEvent) => boolean145export type KeyFilter = true | string | string[] | KeyPredicate146export type KeyStrokeEventName = "keydown" | "keypress" | "keyup"147export interface OnKeyStrokeOptions {148eventName?: KeyStrokeEventName149target?: MaybeRefOrGetter<EventTarget | null | undefined>150passive?: boolean151/**152* Set to `true` to ignore repeated events when the key is being held down.153*154* @default false155*/156dedupe?: MaybeRefOrGetter<boolean>157}158/**159* Listen for keyboard keystrokes.160*161* @see https://vueuse.org/onKeyStroke162*/163export declare function onKeyStroke(164key: KeyFilter,165handler: (event: KeyboardEvent) => void,166options?: OnKeyStrokeOptions,167): () => void168export declare function onKeyStroke(169handler: (event: KeyboardEvent) => void,170options?: OnKeyStrokeOptions,171): () => void172/**173* Listen to the keydown event of the given key.174*175* @see https://vueuse.org/onKeyStroke176* @param key177* @param handler178* @param options179*/180export declare function onKeyDown(181key: KeyFilter,182handler: (event: KeyboardEvent) => void,183options?: Omit<OnKeyStrokeOptions, "eventName">,184): () => void185/**186* Listen to the keypress event of the given key.187*188* @see https://vueuse.org/onKeyStroke189* @param key190* @param handler191* @param options192*/193export declare function onKeyPressed(194key: KeyFilter,195handler: (event: KeyboardEvent) => void,196options?: Omit<OnKeyStrokeOptions, "eventName">,197): () => void198/**199* Listen to the keyup event of the given key.200*201* @see https://vueuse.org/onKeyStroke202* @param key203* @param handler204* @param options205*/206export declare function onKeyUp(207key: KeyFilter,208handler: (event: KeyboardEvent) => void,209options?: Omit<OnKeyStrokeOptions, "eventName">,210): () => void211```212