Vize

on-key-stroke

Run a handler when matching keys are pressed.

Package @vizejs/composable/on-key-stroke
Own the source vize lib pull composable:on-key-stroke
Runtime exports onKeyStroke, onKeyDown, onKeyUp
Gzip budget 3328 B

Usage

import { onKeyStroke, onKeyDown, onKeyUp } from "@vizejs/composable/on-key-stroke";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
onKeyStroke input experimental deterministic-fallback stable caller, reactive-scope web, desktop window normalizeKeyName, tryOnScopeDispose
onKeyDown input experimental deterministic-fallback stable caller, reactive-scope web, desktop window onKeyStroke
onKeyUp input experimental deterministic-fallback stable caller, reactive-scope web, desktop window onKeyStroke

API

onKeyStroke

Run a handler when matching keys are pressed. key accepts a name, a list of names (normalized like combos, so "esc" matches Escape and "a" matches KeyA), a predicate, or true for every key. The listener follows the reactive target and is removed with the owning scope or the returned stop function; nothing is registered during server rendering.

function onKeyStroke( key: KeyFilter | true, handler: (event: KeyboardEvent) => void, options: OnKeyStrokeOptions = {}, ): () => void

onKeyDown

onKeyStroke for keydown.

function onKeyDown( key: KeyFilter | true, handler: (event: KeyboardEvent) => void, options: Omit<OnKeyStrokeOptions, "eventName"> = {}, ): () => void

onKeyUp

onKeyStroke for keyup.

function onKeyUp( key: KeyFilter | true, handler: (event: KeyboardEvent) => void, options: Omit<OnKeyStrokeOptions, "eventName"> = {}, ): () => void

Types

OnKeyStrokeOptions

Options for onKeyStroke.

Member Type Description
eventName? KeyStrokeEventName Keyboard event to listen for.
target? MaybeRefOrGetter<EventTarget | null | undefined> Event target.
passive? boolean Register a passive listener (the handler then cannot preventDefault()).
dedupe? MaybeRefOrGetter<boolean> Ignore auto-repeated keydown events while a key is held.