Skip to Content
Vue Flow v2 is here and joins xyflow! Read more here →

useKeyPress()

Source on GitHub 

The useKeyPress composable returns a reactive boolean that tracks whether a given key or key combination is currently pressed. It is the same key-filter mechanism Vue Flow uses internally for shortcuts like selection and deletion.

<script setup lang="ts"> import { useKeyPress } from '@xyflow/vue'; const shiftPressed = useKeyPress('Shift'); const saveCombo = useKeyPress(['Meta+s', 'Control+s']); </script>

Signature

Parameters:
NameTypeDefault
keyFilterMaybeRefOrGetter<false | KeyFilter | null>

Can be a boolean, a string, an array of strings or a function that returns a boolean. If it’s a boolean, it will act as if the key is always pressed. If it’s a string, it will return true if a key matching that string is pressed. If it’s an array of strings, it will return true if any of the strings match a key being pressed, or a combination (e.g. [‘ctrl+a’, ‘ctrl+b’])

options.targetMaybeRefOrGetter<EventTarget | null | undefined>

The DOM element the key listeners are attached to.

document
options.actInsideInputWithModifierMaybeRefOrGetter<boolean>

A key press is normally ignored while a text input, textarea, or contentEditable element is focused. When true, key combinations that include a modifier (Ctrl/Meta/Shift/Alt) are still detected inside those elements — so a shortcut like Meta+a keeps working while the user is typing.

true
options.preventDefaultMaybeRefOrGetter<boolean>

Whether to call preventDefault() on a matched key event (skipped for a button/link pressed without a modifier). Set to false to leave the event’s default behaviour untouched and handle it yourself.

true
Returns:
ShallowRef<boolean, boolean>

Notes

  • The key filter accepts a single key, an array of keys/combinations, a boolean, or null — and can be a ref or getter so it stays reactive.
  • Options let you scope the listener to a target element, react to combinations while typing in inputs, and control preventDefault.