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: Sensors3variants: onKeyDown, onKeyUp, onKeyPressed4---56# onKeyStroke78Listen for keyboard keystrokes. By default, listens on `keydown` events on `window`.910## Usage1112```ts13import { onKeyStroke } from '@vueuse/core'1415onKeyStroke('ArrowDown', (e) => {16e.preventDefault()17})18```1920See [this table](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) for all key codes.2122### Return Value2324Returns a stop function to remove the event listener.2526```ts27const stop = onKeyStroke('Escape', handler)2829// Later, stop listening30stop()31```3233### Listen To Multiple Keys3435```ts36import { onKeyStroke } from '@vueuse/core'3738onKeyStroke(['s', 'S', 'ArrowDown'], (e) => {39e.preventDefault()40})4142// listen to all keys by passing `true` or skipping the key parameter43onKeyStroke(true, (e) => {44e.preventDefault()45})46onKeyStroke((e) => {47e.preventDefault()48})49```5051### Custom Key Predicate5253You can pass a custom function to determine which keys should trigger the handler.5455```ts56import { onKeyStroke } from '@vueuse/core'5758onKeyStroke(59e => e.key === 'A' && e.shiftKey,60(e) => {61console.log('Shift+A pressed')62},63)64```6566### Custom Event Target6768```ts69import { onKeyStroke } from '@vueuse/core'7071onKeyStroke('A', (e) => {72console.log('Key A pressed on document')73}, { target: document })74```7576### Ignore Repeated Events7778The callback will trigger only once when pressing `A` and **holding down**. The `dedupe` option can also be a reactive ref.7980```ts81import { onKeyStroke } from '@vueuse/core'8283onKeyStroke('A', (e) => {84console.log('Key A pressed')85}, { dedupe: true })86```8788Reference: [KeyboardEvent.repeat](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat)8990### Passive Mode9192Set `passive: true` to use a passive event listener.9394```ts95import { onKeyStroke } from '@vueuse/core'9697onKeyStroke('A', handler, { passive: true })98```99100## Directive Usage101102```vue103<script setup lang="ts">104import { vOnKeyStroke } from '@vueuse/components'105106function onUpdate(e: KeyboardEvent) {107// impl...108}109</script>110111<template>112<input v-on-key-stroke:c,v="onUpdate" type="text">113<!-- with options -->114<input v-on-key-stroke:c,v="[onUpdate, { eventName: 'keyup' }]" type="text">115</template>116```117118### Custom Keyboard Event119120```ts121import { onKeyStroke } from '@vueuse/core'122// ---cut---123onKeyStroke('Shift', (e) => {124console.log('Shift key up')125}, { eventName: 'keyup' })126```127128Or129130```ts131import { onKeyUp } from '@vueuse/core'132// ---cut---133onKeyUp('Shift', () => console.log('Shift key up'))134```135136## Shorthands137138- `onKeyDown` - alias for `onKeyStroke(key, handler, {eventName: 'keydown'})`139- `onKeyPressed` - alias for `onKeyStroke(key, handler, {eventName: 'keypress'})`140- `onKeyUp` - alias for `onKeyStroke(key, handler, {eventName: 'keyup'})`141142## Type Declarations143144```ts145export type KeyPredicate = (event: KeyboardEvent) => boolean146export type KeyFilter = true | string | string[] | KeyPredicate147export type KeyStrokeEventName = "keydown" | "keypress" | "keyup"148export interface OnKeyStrokeOptions {149eventName?: KeyStrokeEventName150target?: MaybeRefOrGetter<EventTarget | null | undefined>151passive?: boolean152/**153* Set to `true` to ignore repeated events when the key is being held down.154*155* @default false156*/157dedupe?: MaybeRefOrGetter<boolean>158}159/**160* Listen for keyboard keystrokes.161*162* @see https://vueuse.org/onKeyStroke163*/164export declare function onKeyStroke(165key: KeyFilter,166handler: (event: KeyboardEvent) => void,167options?: OnKeyStrokeOptions,168): () => void169export declare function onKeyStroke(170handler: (event: KeyboardEvent) => void,171options?: OnKeyStrokeOptions,172): () => void173/**174* Listen to the keydown event of the given key.175*176* @see https://vueuse.org/onKeyStroke177* @param key178* @param handler179* @param options180*/181export declare function onKeyDown(182key: KeyFilter,183handler: (event: KeyboardEvent) => void,184options?: Omit<OnKeyStrokeOptions, "eventName">,185): () => void186/**187* Listen to the keypress event of the given key.188*189* @see https://vueuse.org/onKeyStroke190* @param key191* @param handler192* @param options193*/194export declare function onKeyPressed(195key: KeyFilter,196handler: (event: KeyboardEvent) => void,197options?: Omit<OnKeyStrokeOptions, "eventName">,198): () => void199/**200* Listen to the keyup event of the given key.201*202* @see https://vueuse.org/onKeyStroke203* @param key204* @param handler205* @param options206*/207export declare function onKeyUp(208key: KeyFilter,209handler: (event: KeyboardEvent) => void,210options?: Omit<OnKeyStrokeOptions, "eventName">,211): () => void212```213