packages/core/onStartTyping/index.md
Fires when users start typing on non-editable elements. Useful for auto-focusing an input field when the user starts typing anywhere on the page.
<script setup lang="ts">
import { onStartTyping } from '@vueuse/core'
import { useTemplateRef } from 'vue'
const input = useTemplateRef('input')
onStartTyping(() => {
if (!input.value.active)
input.value.focus()
})
</script>
<template>
<input ref="input" type="text" placeholder="Start typing to focus">
</template>
import { onStartTyping } from '@vueuse/core'
onStartTyping(handleKey, {
// only allow numbers
isTypedCharValid: e => /^\d$/.test(e.key)
})
import { isFocusedElementEditable as defaultEditable, onStartTyping } from '@vueuse/core'
onStartTyping(handleKey, {
isFocusedElementEditable: () => {
const { activeElement } = document
// Exclude elements with id 'targetInput'
if (activeElement?.id === 'targetInput')
return true
return defaultEditable()
}
})
The callback only fires when:
<input>, <textarea>, or contenteditable) is focusedThis allows users to start typing anywhere on the page without accidentally triggering the callback when using keyboard shortcuts or interacting with form fields.
Both isFocusedElementEditable and isTypedCharValid are also exported as utility functions, so you can reuse them when writing custom options.