Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(shortcuts): match non-Latin layouts by physical key (closes #11846)
Elian Doraneda6b6528d92
diff --git a/apps/client/src/services/shortcuts.spec.ts b/apps/client/src/services/shortcuts.spec.tsindex c3ff82cc7c..ced70a61b8 100644--- a/apps/client/src/services/shortcuts.spec.ts+++ b/apps/client/src/services/shortcuts.spec.ts@@ -272,6 +272,46 @@ describe("shortcuts", () => {expect(matchesShortcut(event, "Ctrl+A")).toBe(true);});+ it("matches by physical key on a layout whose keys don't produce Latin characters", () => {+ // Russian ЙЦУКЕН: the J key produces "о", the [ key "х" and the . key "ю".+ const ctrlJ = createKeyboardEvent({ key: "о", code: "KeyJ", ctrlKey: true });+ expect(matchesShortcut(ctrlJ, "Ctrl+J")).toBe(true);+ expect(matchesShortcut(ctrlJ, "Ctrl+O")).toBe(false);++ const ctrlShiftJ = createKeyboardEvent({ key: "О", code: "KeyJ", ctrlKey: true, shiftKey: true });+ expect(matchesShortcut(ctrlShiftJ, "Ctrl+Shift+J")).toBe(true);++ const ctrlBracket = createKeyboardEvent({ key: "х", code: "BracketLeft", ctrlKey: true });+ expect(matchesShortcut(ctrlBracket, "Ctrl+[")).toBe(true);++ const ctrlPeriod = createKeyboardEvent({ key: "ю", code: "Period", ctrlKey: true });+ expect(matchesShortcut(ctrlPeriod, "Ctrl+.")).toBe(true);++ // The layout's own "." sits on the physical / key and still counts as ".".+ const ctrlRussianPeriod = createKeyboardEvent({ key: ".", code: "Slash", ctrlKey: true });+ expect(matchesShortcut(ctrlRussianPeriod, "Ctrl+.")).toBe(true);+ expect(matchesShortcut(ctrlRussianPeriod, "Ctrl+/")).toBe(false);+ });++ it("keeps matching a Latin layout by character, not by physical key", () => {+ // AZERTY: the physical W key produces "z".+ const ctrlZ = createKeyboardEvent({ key: "z", code: "KeyW", ctrlKey: true });+ expect(matchesShortcut(ctrlZ, "Ctrl+Z")).toBe(true);+ expect(matchesShortcut(ctrlZ, "Ctrl+W")).toBe(false);++ // Keys that type a non-ASCII character in a Latin script, or a symbol, keep their own meaning.+ const latinKeys = [+ { key: "ù", code: "Quote", shortcut: "Ctrl+'" }, // French+ { key: "²", code: "Backquote", shortcut: "Ctrl+`" }, // French+ { key: "ö", code: "Semicolon", shortcut: "Ctrl+;" }, // German+ { key: "ı", code: "KeyI", shortcut: "Ctrl+I" } // Turkish+ ];+ for (const { key, code, shortcut } of latinKeys) {+ const event = createKeyboardEvent({ key, code, ctrlKey: true });+ expect(matchesShortcut(event, shortcut), `${key} on ${code}`).toBe(false);+ }+ });+it("should match Alt+letter shortcuts on macOS where key is a special character", () => {// On macOS, pressing Option/Alt + A produces 'å' but code remains 'KeyA'const macOSAltAEvent = createKeyboardEvent({diff --git a/apps/client/src/services/shortcuts.ts b/apps/client/src/services/shortcuts.tsindex 165168581d..b9527cc682 100644--- a/apps/client/src/services/shortcuts.ts+++ b/apps/client/src/services/shortcuts.ts@@ -238,12 +238,47 @@ export function keyMatches(e: KeyboardEvent, key: string): boolean {const expectedCode = `Key${key.toUpperCase()}`;return e.code === expectedCode || e.key.toLowerCase() === key.toLowerCase();}- return e.key.toLowerCase() === key.toLowerCase();+ return e.key.toLowerCase() === key.toLowerCase() || matchesUsPhysicalKey(e, key);}// For regular keys, check both key and code as fallbackreturn e.key.toLowerCase() === key.toLowerCase() ||- e.code.toLowerCase() === key.toLowerCase();+ e.code.toLowerCase() === key.toLowerCase() ||+ matchesUsPhysicalKey(e, key);+}++// `KeyboardEvent.code` of the US QWERTY key that produces each punctuation character.+const usPunctuationCodes: Record<string, string> = {+ "-": "Minus",+ "=": "Equal",+ "[": "BracketLeft",+ "]": "BracketRight",+ "\\": "Backslash",+ ";": "Semicolon",+ "'": "Quote",+ ",": "Comma",+ ".": "Period",+ "/": "Slash",+ "`": "Backquote"+};++// A letter outside the Latin script, such as Cyrillic, Greek or Hebrew.+const NON_LATIN_LETTER = /^(?=\p{L}$)(?![\p{Script=Latin}\p{Script=Common}])/u;++/**+ * Matches a key that types a non-Latin letter by its position on US QWERTY, since such a layout+ * cannot type the shortcut's character at all. Every other key is left to `e.key`, so AZERTY's+ * Ctrl+Z stays on the key labeled Z and French `ù` or German `ö` don't stand in for punctuation.+ */+function matchesUsPhysicalKey(e: KeyboardEvent, key: string): boolean {+ if (!NON_LATIN_LETTER.test(e.key)) {+ return false;+ }++ const expectedCode = key.length === 1 && key >= "a" && key <= "z"+ ? `Key${key.toUpperCase()}`+ : usPunctuationCodes[key];+ return expectedCode !== undefined && e.code === expectedCode;}/**