Files
feishin/src/shared/utils/keyboard-code-to-hotkey.ts
T
Ryan Kupka c301b14cb3 fix: recognize function keys F1–F24 when assigning hotkeys (#2157)
keyboardCodeToHotkeyKey had branches for Key*, Digit*, and Numpad* codes
but none for function keys, so KeyboardEvent.code values "F1".."F24"
returned null and the hotkey capture handler silently dropped them. This
made every function key unassignable (not just F13+ such as F21/F22).

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 00:54:39 -07:00

82 lines
1.7 KiB
TypeScript

const CODE_TO_HOTKEY_KEY: Record<string, string> = {
ArrowDown: 'arrowdown',
ArrowLeft: 'arrowleft',
ArrowRight: 'arrowright',
ArrowUp: 'arrowup',
Backquote: '`',
Backslash: '\\',
Backspace: 'backspace',
BracketLeft: '[',
BracketRight: ']',
Comma: ',',
Delete: 'delete',
End: 'end',
Enter: 'enter',
Equal: 'equal',
Escape: 'escape',
Home: 'home',
Insert: 'insert',
Minus: 'minus',
PageDown: 'pagedown',
PageUp: 'pageup',
Period: '.',
Quote: "'",
Semicolon: ';',
Slash: '/',
Space: 'space',
Tab: 'tab',
};
const NUMPAD_CODE_TO_HOTKEY_KEY: Record<string, string> = {
Add: 'numpadadd',
Decimal: 'numpaddecimal',
Divide: 'numpaddivide',
Enter: 'numpadenter',
Multiply: 'numpadmultiply',
Subtract: 'numpadsubtract',
};
export const MODIFIER_KEY_CODES = new Set([
'AltLeft',
'AltRight',
'ControlLeft',
'ControlRight',
'MetaLeft',
'MetaRight',
'ShiftLeft',
'ShiftRight',
]);
export const keyboardCodeToHotkeyKey = (code: string): null | string => {
const mapped = CODE_TO_HOTKEY_KEY[code];
if (mapped) {
return mapped;
}
if (code.startsWith('Key')) {
return code.slice(3).toLowerCase();
}
if (code.startsWith('Digit')) {
return code.slice(5);
}
if (/^F([1-9]|1\d|2[0-4])$/.test(code)) {
return code.toLowerCase();
}
if (code.startsWith('Numpad')) {
const suffix = code.slice(6);
const numpadMapped = NUMPAD_CODE_TO_HOTKEY_KEY[suffix];
if (numpadMapped) {
return numpadMapped;
}
if (/^\d$/.test(suffix)) {
return `numpad${suffix}`;
}
}
return null;
};