@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
46 lines • 1.98 kB
TypeScript
import { Store } from "./_store.js";
/**
* A keyboard layout: each physical code mapped to the character it types
* without modifiers, such as `KeyY` to `z` on a German layout.
*/
export type KeyboardLayoutMap = ReadonlyMap<string, string>;
/**
* The store that holds the browser's keyboard layout once `loadKeyboardLayout`
* has loaded it. Framework adapters read it to label physical bindings, so
* `[KeyQ]` shows as `A` on a French AZERTY keyboard, and devtools show it.
* Matching and recording never read it.
*
* The state is `null` until the layout loads, and stays `null` where the
* browser has no layout API: Firefox, Safari, servers, and cross-origin frames
* without the `keyboard-map` permission. Pass the state to `formatForDisplay`
* as `layoutMap` when you format labels yourself.
*/
export declare function getKeyboardLayoutStore(): Store<KeyboardLayoutMap | null>;
/**
* Loads the browser's keyboard layout into `getKeyboardLayoutStore()`, with
* `navigator.keyboard.getLayoutMap()`. Only Chromium browsers have it, so the
* layout stays `null` elsewhere and labels keep their conventional form.
*
* Framework adapters call this for you when a label or layout primitive
* mounts. Call it yourself only without an adapter, in the browser, after
* your page has rendered.
*
* The first caller reads the layout. Browsers send no event when the user
* switches layouts, so while any caller is active, the layout is read again
* each time the window regains focus.
*
* @returns A function that ends this caller's interest. When the last caller
* stops, refreshing stops. The loaded layout stays in the store, so the next
* label has it at once.
*
* @example
* ```ts
* const stop = loadKeyboardLayout()
* getKeyboardLayoutStore().subscribe((layoutMap) => {
* label.textContent = formatForDisplay('[KeyW]', {
* layoutMap: layoutMap ?? undefined,
* })
* })
* ```
*/
export declare function loadKeyboardLayout(): () => void;