add physical key mapping for useHotkeys to support alt keyboard languages (#2051)

This commit is contained in:
jeffvli
2026-05-26 21:55:08 -07:00
parent 57b11e0dae
commit e206136156
5 changed files with 130 additions and 16 deletions
+37
View File
@@ -0,0 +1,37 @@
import type { HotkeyItem } from '@mantine/hooks';
const RESERVED_KEYS = new Set(['alt', 'ctrl', 'meta', 'mod', 'shift']);
/**
* Converts stored hotkey strings to Mantine's physical-key format.
* Mantine matches KeyboardEvent.code via normalizeKey, which turns Digit1 into
* "digit1" but leaves "1" as "1" — so mod+1 must become mod+Digit1.
*/
export const toPhysicalHotkey = (hotkey: string): string =>
hotkey
.split('+')
.map((part) => part.trim())
.map((part) => {
if (part === '[plus]') {
return part;
}
const lower = part.toLowerCase();
if (RESERVED_KEYS.has(lower)) {
return lower;
}
if (/^\d$/.test(part)) {
return `Digit${part}`;
}
return part;
})
.join('+');
export const withPhysicalKeys = (hotkeys: HotkeyItem[]): HotkeyItem[] =>
hotkeys.map(([hotkey, handler, options]) => [
toPhysicalHotkey(hotkey),
handler,
{ ...options, usePhysicalKeys: true },
]);