useKeyboardShortcut
zyeon/use-keyboard-shortcutFreeHook
A hook that binds human-readable key combos like mod+k to a handler, with platform-aware modifiers and an input-focus guard.
Install it
npx shadcn@latest add https://ui.zyeon.ai/r/use-keyboard-shortcut.jsonSource
1"use client"23import * as React from "react"45export interface UseKeyboardShortcutOptions {6 /** Turn the listener off (e.g. a shortcut that only applies while a panel is open). Defaults to true. */7 enabled?: boolean8 /** Call `event.preventDefault()` on a match, blocking the browser's own behaviour (⌘S saves the page, ⌘K jumps to the address bar). Defaults to true. */9 preventDefault?: boolean10 /** Don't fire while focus is in an input / textarea / select / contenteditable. Defaults to true. */11 ignoreInputs?: boolean12 /**13 * Where to attach the listener, `window` by default. Pass a node, or a ref object — the latter is14 * the only reliable way to scope a shortcut to a container: `ref.current` is still null on the15 * first render and assigning to a ref triggers no re-render, so passing `ref.current` leaves the16 * listener on window forever.17 */18 target?: EventTarget | React.RefObject<EventTarget | null> | null19}2021interface ParsedShortcut {22 key: string23 mod: boolean24 ctrl: boolean25 meta: boolean26 shift: boolean27 alt: boolean28}2930/** Human spelling → the lower-cased `KeyboardEvent.key` value. */31const KEY_ALIASES: Record<string, string> = {32 esc: "escape",33 space: " ",34 spacebar: " ",35 plus: "+",36 up: "arrowup",37 down: "arrowdown",38 left: "arrowleft",39 right: "arrowright",40 return: "enter",41 del: "delete",42}4344/** Parses "mod+shift+k" into a structured shortcut; returns null when only modifiers were given and no actual key. */45function parseShortcut(combo: string): ParsedShortcut | null {46 const parsed: ParsedShortcut = {47 key: "",48 mod: false,49 ctrl: false,50 meta: false,51 shift: false,52 alt: false,53 }5455 const parts = combo56 .toLowerCase()57 .split("+")58 .map(part => part.trim())59 .filter(Boolean)6061 for (const part of parts) {62 switch (part) {63 case "mod":64 parsed.mod = true65 break66 case "ctrl":67 case "control":68 parsed.ctrl = true69 break70 case "meta":71 case "cmd":72 case "command":73 parsed.meta = true74 break75 case "shift":76 parsed.shift = true77 break78 case "alt":79 case "option":80 parsed.alt = true81 break82 default:83 parsed.key = KEY_ALIASES[part] ?? part84 }85 }8687 return parsed.key ? parsed : null88}8990/** `mod` is ⌘ on Apple platforms and Ctrl elsewhere. Called from effects only — navigator is never read during render. */91// … truncated
Files it writes
More from zyeon
All 893 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useAsyncuse-async | zyeon | Hooks | Free | no deps | |
| useBatteryuse-battery | zyeon | Hooks | Free | no deps | |
| useBroadcastChanneluse-broadcast-channel | zyeon | Hooks | Free | no deps | |
| useClickOutsideuse-click-outside | zyeon | Hooks | Free | no deps | |
| useClipboardPasteuse-clipboard-paste | zyeon | Hooks | Free | no deps | |
| useControllableStateuse-controllable-state | zyeon | Hooks | Free | no deps | |
| useCookieuse-cookie | zyeon | Hooks | Free | no deps | |
| useCopyToClipboarduse-copy-to-clipboard | zyeon | Hooks | Free | no deps |
