Skip to content

Commit 664499c

Browse files
authored
Limit physical key fallback to non-Latin layout output (pingdotgg#4469)
1 parent 931e915 commit 664499c

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎apps/web/src/keybindings.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,35 @@ describe("resolveShortcutCommand", () => {
728728
"rightPanel.toggle",
729729
);
730730
});
731+
732+
it("matches non-Latin layout letters using the physical key code", () => {
733+
const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
734+
735+
assert.strictEqual(
736+
resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
737+
platform: "MacIntel",
738+
}),
739+
"diff.toggle",
740+
);
741+
});
742+
743+
it("ignores the physical key code when the layout types a different Latin letter", () => {
744+
const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
745+
746+
// On a remapped layout the physical D key types "a"; only the physical
747+
// key whose layout output is "d" may trigger the shortcut.
748+
assert.isNull(
749+
resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
750+
platform: "MacIntel",
751+
}),
752+
);
753+
assert.strictEqual(
754+
resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
755+
platform: "MacIntel",
756+
}),
757+
"diff.toggle",
758+
);
759+
});
731760
});
732761

733762
describe("formatShortcutLabel", () => {

‎apps/web/src/keybindings.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ function normalizeEventKey(key: string): string {
7171
}
7272

7373
function resolveEventKeys(event: ShortcutEventLike): Set<string> {
74-
const keys = new Set([normalizeEventKey(event.key)]);
74+
const layoutKey = normalizeEventKey(event.key);
75+
const keys = new Set([layoutKey]);
76+
// The physical-position fallback exists for layouts that type non-Latin
77+
// letters (Cyrillic, Greek) and for Option-modified symbols on macOS.
78+
// When the layout already produces a Latin letter, match on it alone;
79+
// otherwise a remapped physical key triggers shortcuts for two different
80+
// letters at once and shadows system shortcuts on non-QWERTY layouts.
7581
const letterCode = event.code?.match(/^Key([A-Z])$/)?.[1];
76-
if (letterCode) {
82+
if (letterCode && !/^[a-z]$/.test(layoutKey)) {
7783
keys.add(letterCode.toLowerCase());
7884
}
7985
const aliases = event.code ? EVENT_CODE_KEY_ALIASES[event.code] : undefined;

0 commit comments

Comments
 (0)