@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
166 lines (165 loc) • 4.42 kB
JavaScript
// src/_constants.ts
/** The scope of hotkeys that do not name one. It is always active unless an exclusive scope is active. */
const GLOBAL_SCOPE = "global";
/**
* Default options for hotkey/sequence registration.
* Omitted: platform, target (resolved at registration), requireReset (HotkeyManager only).
*/
const defaultHotkeyOptions = {
capture: false,
preventDefault: true,
stopPropagation: true,
eventType: "keydown",
enabled: true,
ignoreInputs: "auto",
conflictBehavior: "warn",
scope: GLOBAL_SCOPE
};
/**
* Physical codes, other than letters, digits, and punctuation, that the
* logical matcher falls back to: the numpad operators, the numpad Enter, and
* the space bar. Matching depends on this list, so a code added here starts
* matching a logical key.
*/
const NAMED_CODE_KEYS = {
NumpadAdd: "+",
NumpadSubtract: "-",
NumpadMultiply: "*",
NumpadDivide: "/",
NumpadDecimal: ".",
NumpadEnter: "Enter",
Space: "Space"
};
/**
* Fallback labels for keys with no US equivalent, from the legends the UI
* Events code spec gives: `\` on UK ISO keyboards, and `\` (ろ) and `¥` on
* Japanese keyboards. `formatForDisplay` and `formatForAria` use them;
* matching never does.
*/
const INTL_KEY_LABELS = {
IntlBackslash: "\\",
IntlRo: "\\",
IntlYen: "¥"
};
/**
* Numpad codes whose key value differs from the code name, beyond the digits
* and `NAMED_CODE_KEYS`. `formatForAria` names them, and character-key
* suspension treats the ones that type a character as character keys. They
* stay out of `NAMED_CODE_KEYS`, so a logical `(` hotkey doesn't start
* matching `[NumpadParenLeft]`.
*/
const NUMPAD_KEY_VALUES = {
NumpadBackspace: "Backspace",
NumpadClear: "Clear",
NumpadComma: ",",
NumpadEqual: "=",
NumpadHash: "#",
NumpadParenLeft: "(",
NumpadParenRight: ")",
NumpadStar: "*"
};
/**
* Codes that are not also key values, such as `NumpadMemoryAdd` or `Lang1`.
* Every other supported code, such as `Enter` or `F1`, names its key.
*/
const CODE_ONLY_NAMES = /^(?:Numpad\w*|Lang\d|LaunchApp\d|MediaSelect|Sleep|Turbo|Abort|Resume|Suspend)$/;
/** What US Shift types on each punctuation key, and the key itself. */
const US_SHIFTED = "~_+{}|:\"<>?";
const US_UNSHIFTED = "`-=[]\\;',./";
/**
* Function keys (`F1` to `F24`) and media keys: the audio, browser, launch,
* and playback keys. They type nothing, and a text field does nothing with them.
*/
const NON_TYPING_KEY = /^(F([1-9]|1\d|2[0-4])$|Audio|Browser|Launch|Media)/;
const VERTICAL_KEYS = ["ArrowUp", "ArrowDown"];
const HORIZONTAL_KEYS = ["ArrowLeft", "ArrowRight"];
const ENTER_KEYS = ["Enter"];
const SPACE_KEYS = ["Space"];
const ACTIVATION_KEYS = ["Enter", "Space"];
const TYPEAHEAD = "typeahead";
const MENU_KEYS = [
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"Home",
"End",
"Enter",
"Space",
TYPEAHEAD
];
const COMBOBOX_KEYS = [
"ArrowUp",
"ArrowDown",
"Home",
"End",
"Enter",
"Space",
TYPEAHEAD
];
const GRID_KEYS = [
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"Home",
"End",
"PageUp",
"PageDown",
"Enter",
"Space"
];
const RADIO_KEYS = [
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"Space"
];
const NAVIGATION_KEYS = [
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"Home",
"End",
"PageUp",
"PageDown"
];
const SPIN_KEYS = [
"ArrowUp",
"ArrowDown",
"Home",
"End",
"PageUp",
"PageDown"
];
const CARET_KEYS = [
"ArrowLeft",
"ArrowRight",
"Home",
"End"
];
/**
* Input types that aren't text fields, with the keys each one keeps. Browsers
* report unknown types as `'text'`.
*/
const INPUT_KEYS = {
button: ACTIVATION_KEYS,
submit: ACTIVATION_KEYS,
reset: ACTIVATION_KEYS,
image: ACTIVATION_KEYS,
color: ACTIVATION_KEYS,
file: ACTIVATION_KEYS,
checkbox: SPACE_KEYS,
radio: RADIO_KEYS,
range: NAVIGATION_KEYS
};
const SERVER_AGENT = /^(?:Node\.js\/|Deno\/|Bun\/|Cloudflare-Workers)/;
/** The recorder's state while it isn't recording and has recorded nothing. */
const INITIAL_RECORDER_STATE = {
isRecording: false,
steps: [],
recordedHotkey: null
};
export { ACTIVATION_KEYS, CARET_KEYS, CODE_ONLY_NAMES, COMBOBOX_KEYS, ENTER_KEYS, GLOBAL_SCOPE, GRID_KEYS, HORIZONTAL_KEYS, INITIAL_RECORDER_STATE, INPUT_KEYS, INTL_KEY_LABELS, MENU_KEYS, NAMED_CODE_KEYS, NAVIGATION_KEYS, NON_TYPING_KEY, NUMPAD_KEY_VALUES, RADIO_KEYS, SERVER_AGENT, SPACE_KEYS, SPIN_KEYS, TYPEAHEAD, US_SHIFTED, US_UNSHIFTED, VERTICAL_KEYS, defaultHotkeyOptions };