Skip to Content
ohooks

useHotkeys

Listens for keyboard shortcut combinations (including cross-platform mod support) and invokes a callback when matched.

Loading preview...

The useHotkeys hook binds one or more keyboard shortcut combinations (such as 'mod+k', 'shift+?', or ['ctrl+s', 'meta+s']) to a callback, ignoring editable form fields by default.

Installation

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

Usage

Examples

Preventing Default Browser Shortcuts (mod+s)

Set preventDefault: true and enableOnFormTags: true to intercept Cmd+S / Ctrl+S even while the user is typing inside an <input> or <textarea>.

Scoping Shortcuts to a Container Element

Pass a target ref in the options object so the shortcut only triggers when focus is inside that container.

API Reference

UseHotkeysOptions

Configuration options for hotkey matching, event type, and form element behavior.

Property
Specification
enabled?
uniondefault:true
Whether the hotkey listener is active
preventDefault?
uniondefault:true
Call event.preventDefault() when a matching hotkey is pressed
stopPropagation?
uniondefault:false
Call event.stopPropagation() when a matching hotkey is pressed
enableOnFormTags?
uniondefault:false
Allow hotkeys to trigger while focus is inside <input>, <textarea>, <select>, or contentEditable elements
event?
uniondefault:'keydown'
Keyboard event type to listen for
target?
uniondefault:window
Target element, document, or window to attach the listener to