Todae

Todae

useHotkey

useHotkey(hotkey, handler, options) runs handler when a key pressed anywhere on the page matches hotkey. It listens for keydown on the document and prevents the default of a key it matches, so the browser's own shortcut does not also run.

On this page

Press Alt+T. The hook listens on the document, so the shortcut runs with focus anywhere on the page, and the button names it for assistive technology.

import { Button, Stack, Text, useHotkey } from '@tounsoo/todae';
import { useState } from 'react';

export default function SearchShortcut() {
  const [open, setOpen] = useState(false);
  const keys = ['Alt+T'];
  useHotkey(keys, () => setOpen(true));
  return (
    <Stack.Root gap="condensed" align="start">
      <Button.Root aria-keyshortcuts={keys.join(' ')} onClick={() => setOpen(true)}>
        Search
      </Button.Root>
      <Text.Root>{open ? 'Search is open' : 'Press Alt+T'}</Text.Root>
    </Stack.Root>
  );
}

Press Alt+T

useHotkey(['Control+K', 'Meta+K'], () => setOpen(true));

Write a hotkey as modifiers and a key joined by +, such as Alt+T or F8. Modifiers are Alt, Control, Meta and Shift, which can also be written as Option, Ctrl or Cmd, and they must match exactly, so Control+K does not run on Ctrl+Shift+K. A letter or digit matches what the key types. When the key types no Latin letter or digit, as on a Korean layout, it matches the key in that letter's place. A hotkey with a modifier name the hook does not know is off, and logs an error in development.

The hook skips a key something else already prevented, a key pressed while an input method composes text, and a held key that repeats. In a text field it runs only a function key, or a chord with Meta or with Control but not Alt; every other hotkey, such as Shift+A, Escape or an arrow key, is left to the field.

The latest handler runs, so an inline function is fine. Pass { enabled: false } to stop listening. Command palette opens with this hook.

Tell assistive technology about a shortcut with aria-keyshortcuts on the control it stands for.

Accessible composition

Todae handles: Listening on the document, matching modifiers exactly, preventing the browser's default on a match, and leaving typing keys to text fields and input methods, as described above. It sets no attribute and announces nothing.

You supply: aria-keyshortcuts on the control the shortcut stands for, with each hotkey separated by a space. The names Alt, Control, Meta and Shift are valid there too, so one list can feed both; the aliases Ctrl, Cmd and Option are not. Show the shortcut in visible text as well, such as in the control's tooltip or a help page.

const keys = ['Control+K', 'Meta+K'];
useHotkey(keys, () => setOpen(true));

<Button.Root aria-keyshortcuts={keys.join(' ')} onClick={() => setOpen(true)}>
  Search
</Button.Root>

Watch for: A hotkey that is a single letter, digit or symbol, with or without Shift. Outside a text field it runs on one key press, which speech input can set off by accident; give people a way to turn it off with { enabled: false }. A match prevents the browser's default, so a hotkey such as Control+F replaces the browser's own find.

When to use

When: A page-wide shortcut, such as one that opens a search or command palette.

When not: Keys inside a widget, such as arrows in a list → the widget's own onKeyDown. Moving focus to toasts → Toast.Region's hotkey.

Props

useHotkey props
NameTypeDescription
hotkey Requiredstring | readonly string[]

A key with its modifiers, such as "Control+K", "Alt+T" or "F8". Modifiers must match exactly. A list matches any of them. One with a modifier name it does not know is off.

handler Required(event: KeyboardEvent) => void

Runs on a match. An inline function is fine; the latest one is read at key time.

options.enabledboolean

Listen only while true.

Default: true