Skip to Content
ohooks

useKeyPress

Tracks whether a specific key, hotkey combination, or custom key predicate is currently held down.

Loading preview...

The useKeyPress hook listens for keydown and keyup events (and resets automatically on window blur) to return true while a matching key, shortcut combo, or predicate is actively pressed.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useKeyPress.json

Usage

Examples

Multi-Key Combinations and Aliases

Pass a key chord (such as 'mod+k' or 'shift+enter') or an array of keys (['ArrowUp', 'w']) to track whether any matching key is held down.

Custom Key Predicate Function

Pass a predicate function (event: KeyboardEvent) => boolean for custom key matching logic, such as detecting any numeric digit key.

API Reference

UseKeyPressOptions

Configuration options for target element, form tag filtering, and default behavior.

Property
Specification
target?
uniondefault:window
Target element, document, or window to listen on
preventDefault?
uniondefault:false
Call event.preventDefault() when a matching key is pressed