bindKey
Binds a keyboard combo or sequence to a handler and returns an idempotent unbind function. Supports modifiers (ctrl, alt, shift, meta/cmd, mod), multi-step sequences (g i), per-element scoping, automatic input filtering, and configurable repeat / preventDefault behavior.
Browser-only utility — does not work in Node.js or other non-DOM environments.
Try it
Section titled “Try it”Import
Section titled “Import”import { bindKey } from "1o1-utils";import { bindKey } from "1o1-utils/bind-key";Signature
Section titled “Signature”function bindKey( combo: string, handler: (event: KeyboardEvent) => void, options?: BindKeyOptions,): () => voidParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
combo | string | Yes | Key combo (e.g. "ctrl+k") or sequence (e.g. "g i"). Case-insensitive. mod aliases meta on macOS, ctrl elsewhere. |
handler | (event: KeyboardEvent) => void | Yes | Called with the final KeyboardEvent when the combo or full sequence matches. |
options.target | EventTarget | No | Element or Document/Window to attach to. Defaults to window. |
options.filterInputs | boolean | No | Skip handler when focus is on an input-like element (default true). Combos with a non-shift modifier always fire regardless. |
options.sequenceTimeout | number | No | Idle ms after which a sequence buffer resets. Default 1000. |
options.preventDefault | boolean | No | Call event.preventDefault() on a full match. Default false. |
options.ignoreRepeat | boolean | No | Ignore auto-repeat events (event.repeat === true). Default true. |
Returns
Section titled “Returns”() => void — Idempotent unbind function. Safe to call multiple times.
Examples
Section titled “Examples”import { bindKey } from "1o1-utils/bind-key";
const unbindSearch = bindKey("ctrl+k", (e) => { e.preventDefault(); openSearch();});
const unbindGotoInbox = bindKey("g i", () => goToInbox());const unbindEsc = bindKey("escape", () => closeModal());const unbindArrows = bindKey("ctrl+arrowleft", () => prevTab());
// Cleanup on teardownunbindSearch();unbindGotoInbox();unbindEsc();unbindArrows();// React effectuseEffect(() => bindKey("mod+s", saveDocument, { preventDefault: true }), []);Edge Cases
Section titled “Edge Cases”- Throws if
combois empty or malformed. - Throws if
handleris not a function. - The
modalias resolves tometaon macOS (navigator.platform) andctrlelsewhere — keep cross-platform shortcuts portable. - Sequences reset after
sequenceTimeoutms of inactivity. - Inputs (
input,textarea,select,contenteditable) skip the handler unless the combo includes a non-shift modifier. - Auto-repeat (
event.repeat) is ignored by default — passignoreRepeat: falseto fire on every repeat. - Returns a no-op unbinder if
window/documentare missing (e.g. SSR).
Also known as
Section titled “Also known as”keyboard shortcut, hotkey, key binding, keypress, keymap, accelerator, shortcut, mousetrap
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use bindKey to wire ctrl+k to open a command palette in a React app.