Skip to content
filedom
Esc
↑↓navigate↵open⌘Jpreview
On this page

useKeypress

Subscribes to normalized global keydown events.

import { useKeypress } from 'filedom';

const { lastKey } = useKeypress({
  onKey: (key) => {
    if (key === 'r') restart();
  },
});

Arrow keys arrive as up, down, left and right, Return as enter and Space as space. Other keys arrive as their lowercased character. The listener only reports keydowns, so there is no keyup or held-key state.

Options

PropType
disableTerminalEcho?boolean

Turn off terminal echo while mounted and turn it back on at unmount.

Typeboolean
onKey?((key: string, event: GlobalInputEvent) => void)

Receives the normalized key and the raw GlobalInputEvent. Not called for keys with no name, such as a bare modifier.

Type((key: string, event: GlobalInputEvent) => void)

Returns

{ lastKey: string | null, lastEvent: GlobalInputEvent | null }. Both start as null, and lastKey is null after a key with no name.

Was this page helpful?