Skip to content
TypeScript

fix(web): hotkey settings record plain keys and Tab (#14548)

Theo Browne71a90ae70e3f
diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
index 791ebccfa93..15561e06be8 100644
--- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts
@@ -113,6 +113,31 @@ describe("KeybindingsSettings.logic", () => {
).toBe("mod+shift+k");
});
+ it.each([
+ ["k", "KeyK", "k"],
+ ["Tab", "Tab", "tab"],
+ ["F5", "F5", "f5"],
+ ])("captures %s without a modifier", (key, code, expected) => {
+ const noModifiers = { metaKey: false, ctrlKey: false, altKey: false, shiftKey: false };
+ expect(keybindingFromKeyboardEvent({ key, code, ...noModifiers }, "MacIntel")).toBe(expected);
+ });
+
+ it("waits for a key when only a modifier is pressed", () => {
+ expect(
+ keybindingFromKeyboardEvent(
+ {
+ key: "Meta",
+ code: "MetaLeft",
+ metaKey: true,
+ ctrlKey: false,
+ altKey: false,
+ shiftKey: false,
+ },
+ "MacIntel",
+ ),
+ ).toBeNull();
+ });
+
it.each([
["@", "Digit2", "mod+shift+2"],
['"', "Digit2", "mod+shift+2"],
diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.ts
index a1f445b5db8..4f41e375538 100644
--- a/apps/web/src/components/settings/KeybindingsSettings.logic.ts
+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.ts
@@ -358,6 +358,7 @@ function normalizeShortcutKeyToken(key: string): string | null {
return null;
}
+/** Turns a keydown into a binding such as `mod+shift+k` or `tab`. Null for modifier-only presses. */
export function keybindingFromKeyboardEvent(
event: Pick<KeyboardEvent, "key" | "code" | "metaKey" | "ctrlKey" | "altKey" | "shiftKey">,
platform: string,
@@ -375,9 +376,6 @@ export function keybindingFromKeyboardEvent(
}
if (event.altKey) parts.push("alt");
if (event.shiftKey) parts.push("shift");
- if (parts.length === 0) {
- return null;
- }
parts.push(keyToken);
return parts.join("+");
}
diff --git a/apps/web/src/components/settings/KeybindingsSettings.tsx b/apps/web/src/components/settings/KeybindingsSettings.tsx
index 9d768d7fd41..bf14e709dda 100644
--- a/apps/web/src/components/settings/KeybindingsSettings.tsx
+++ b/apps/web/src/components/settings/KeybindingsSettings.tsx
@@ -768,7 +768,8 @@ function useKeybindingRowEditor({
};
const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => {
- if (event.key === "Tab") return;
+ // Tab is recorded like any key while recording; after that it moves focus on.
+ if (event.key === "Tab" && !isRecording) return;
event.preventDefault();
if (event.key === "Escape") {
setDraft({ keyDraft: row.key, isRecording: false });
@@ -1083,7 +1084,8 @@ function useNewKeybindingDraft({
};
const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => {
- if (event.key === "Tab") return;
+ // Tab is recorded like any key while recording; after that it moves focus on.
+ if (event.key === "Tab" && !isRecording) return;
event.preventDefault();
if (event.key === "Escape") {
setDraft({ keyDraft: "", isRecording: false });
diff --git a/apps/web/src/components/settings/useSnapShotShortcutRecorder.tsx b/apps/web/src/components/settings/useSnapShotShortcutRecorder.tsx
index 8f377842b68..50fd3b33821 100644
--- a/apps/web/src/components/settings/useSnapShotShortcutRecorder.tsx
+++ b/apps/web/src/components/settings/useSnapShotShortcutRecorder.tsx
@@ -106,6 +106,8 @@ export function useSnapShotShortcutRecorder({
}
return;
}
+ // A global shortcut without a modifier would take that key from every app.
+ if (!event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey) return;
const input = keybindingFromKeyboardEvent(event, navigator.platform);
if (!input) return;
const next = parseKeybindingShortcut(input);