VueUse onStartTyping 当在不可编辑的元素输入内容时触发的事件

VueUse onStartTyping 当在不可编辑的元素输入内容时触发的事件,比如你当前焦点在div元素,这时你输入a字符可以将焦点设置在输入框,并将a赋值给输入框。

代码示例

<template> 
 <div>
  <input ref="input" type="text" placeholder="输入事件获取焦点">
 </div> 
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { onStartTyping } from '@vueuse/core'
/*
vueuse 中文文档
https://www.itxst.com/vueuse/tutorial.html
*/
const input = ref(null);

onStartTyping(() => {
      if (!input.value.active)
        input.value.focus()
 })
</script>

在线例子

例子

类型定义

/**
 * Fires when users start typing on non-editable elements.
 *
 * @see https://vueuse.org/onStartTyping
 * @param callback
 * @param options
 */
export declare function onStartTyping(
  callback: (event: KeyboardEvent) => void,
  options?: ConfigurableDocument,
): void