Skip to content

onKeyStroke

Category
Export Size
651 B
Last Changed
15 minutes ago

监听键盘按键事件。

Demo

使用箭头键或 w a s d 键来控制球的移动。

在按下键`d`或`->`时重复的事件将被忽略。

用法

js
import { onKeyStroke } from '@vueuse/core'

onKeyStroke('ArrowDown', (e) => {
  e.preventDefault()
})

请参阅此表格获取所有按键代码。

监听多个按键

js
import { onKeyStroke } from '@vueuse/core'

onKeyStroke(['s', 'S', 'ArrowDown'], (e) => {
  e.preventDefault()
})

// 通过 [true / 略过键定义] 监听所有键
onKeyStroke(true, (e) => {
  e.preventDefault()
})
onKeyStroke((e) => {
  e.preventDefault()
})

自定义事件目标

js
onKeyStroke('A', (e) => {
  console.log('按键 A 在文档上被按下')
}, { target: document })

忽略重复事件

当按下 A 键并保持按下时,回调函数只会触发一次。

js
import { onKeyStroke } from '@vueuse/core'

// 使用 `autoRepeat` 选项
onKeyStroke('A', (e) => {
  console.log('按键 A 被按下')
}, { dedupe: true })

参考:KeyboardEvent.repeat

指令用法

vue
<script setup lang="ts">
import { vOnKeyStroke } from '@vueuse/components'

function onUpdate(e) {
  // 实现...
}
</script>

<template>
  <input v-on-key-stroke:c,v="onUpdate" type="text">
  <!-- 带有选项 -->
  <input v-on-key-stroke:c,v="[onUpdate, { eventName: 'keyup' }]" type="text">
</template>

自定义键盘事件

js
onKeyStroke('Shift', (e) => {
  console.log('按键 Shift 松开')
}, { eventName: 'keyup' })

或者

js
onKeyUp('Shift', () => console.log('按键 Shift 松开'))

快捷方式

  • onKeyDown - onKeyStroke(key, handler, {eventName: 'keydown'}) 的别名
  • onKeyPressed - onKeyStroke(key, handler, {eventName: 'keypress'}) 的别名
  • onKeyUp - onKeyStroke(key, handler, {eventName: 'keyup'}) 的别名

类型声明

显示类型声明
typescript
export type KeyPredicate = (event: KeyboardEvent) => boolean
export type KeyFilter = true | string | string[] | KeyPredicate
export type KeyStrokeEventName = "keydown" | "keypress" | "keyup"
export interface OnKeyStrokeOptions {
  eventName?: KeyStrokeEventName
  target?: MaybeRefOrGetter<EventTarget | null | undefined>
  passive?: boolean
  /**
   * 当按键持续按下时,设置为`true`以忽略重复事件。
   *
   * @default false
   */
  dedupe?: MaybeRefOrGetter<boolean>
}
/**
 * 监听键盘按键事件。
 *
 * @see https://vueuse.org/onKeyStroke
 */
export declare function onKeyStroke(
  key: KeyFilter,
  handler: (event: KeyboardEvent) => void,
  options?: OnKeyStrokeOptions,
): () => void
export declare function onKeyStroke(
  handler: (event: KeyboardEvent) => void,
  options?: OnKeyStrokeOptions,
): () => void
/**
 * 监听指定按键的 keydown 事件。
 *
 * @see https://vueuse.org/onKeyStroke
 * @param key
 * @param handler
 * @param options
 */
export declare function onKeyDown(
  key: KeyFilter,
  handler: (event: KeyboardEvent) => void,
  options?: Omit<OnKeyStrokeOptions, "eventName">,
): () => void
/**
 * 监听指定按键的 keypress 事件。
 *
 * @see https://vueuse.org/onKeyStroke
 * @param key
 * @param handler
 * @param options
 */
export declare function onKeyPressed(
  key: KeyFilter,
  handler: (event: KeyboardEvent) => void,
  options?: Omit<OnKeyStrokeOptions, "eventName">,
): () => void
/**
 * 监听指定按键的 keyup 事件。
 *
 * @see https://vueuse.org/onKeyStroke
 * @param key
 * @param handler
 * @param options
 */
export declare function onKeyUp(
  key: KeyFilter,
  handler: (event: KeyboardEvent) => void,
  options?: Omit<OnKeyStrokeOptions, "eventName">,
): () => void

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
webfansplz
Arthur Darkstone
Anonimus Programus
yn
David Hewson
lainbo
jack zhang
vaakian X
vaakian X
azaleta
Curt Grimes
Jelf
leovoon
Arpit
Ivan Que
Alex Kozack
yang

变更日志

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.2 on 3/28/2023
22ca5 - feat: support ignoring repeated stroke (#2652)
v9.10.0 on 1/3/2023
28614 - fix: make target ref nullable (#2579)

Released under the MIT License.