Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: match letter shortcuts by the letter the layout types (#14113)

Sourav Rajvi78e5eefa4d4b
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 68206cc8aef..80cc57172cf 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -225,8 +225,12 @@ export const BUDGET = {
// transfer (6,819,958 -> 6,824,273 B raw, 2,052,278 -> 2,053,773 B transfer against e2a214bc08, one machine and
// build, Node 24, chunk count unchanged at 95): the refusal and evicted-model toast helpers are imported by the
// eager chat, image and video chunks. Raised by that growth.
- transferBytes: 2_051_590,
- rawBytes: 6_818_878,
+ // Matching letter shortcuts by the letter the layout types (#14113) adds 328 B raw / 137 B transfer
+ // (6,809,493 -> 6,809,821 B raw, 2,049,157 -> 2,049,294 B transfer against a6da154b3, one machine and build,
+ // Node 24, chunk count unchanged at 95): the matcher runs on every keydown from the first screen, so there is
+ // no separate module to lazy-load. Raised by that growth.
+ transferBytes: 2_051_727,
+ rawBytes: 6_819_206,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/settings/lib/keyboard-shortcuts.ts b/studio/frontend/src/features/settings/lib/keyboard-shortcuts.ts
index 8fa832dc635..e1f7d1a8246 100644
--- a/studio/frontend/src/features/settings/lib/keyboard-shortcuts.ts
+++ b/studio/frontend/src/features/settings/lib/keyboard-shortcuts.ts
@@ -90,8 +90,7 @@ export interface ShortcutDef {
webOnly?: boolean;
}
-/** `code` is KeyboardEvent.code, so a binding survives a layout change. `mod`
- * is Cmd on macOS and Ctrl elsewhere. */
+/** `code` as eventCode reads it; `mod` is Cmd on macOS and Ctrl elsewhere. */
export interface ShortcutBinding {
code: string;
mod: boolean;
@@ -490,7 +489,7 @@ export function bindingFromEvent(
},
mac = isMacPlatform(),
): ShortcutBinding | null {
- const code = event.code || keyToCode(event.key ?? "");
+ const code = eventCode(event, mac);
if (!code || isModifierCode(code)) return null;
// Off macOS there is nowhere to put Meta: matchesBinding rejects an event carrying it, so
// recording Super+Alt+K would persist plain Alt+K -- a chord the user did not choose, which then
@@ -541,6 +540,33 @@ function keyToCode(key: string): string {
return punctuation[key] ?? key;
}
+function typedPunctuation(
+ event: { code: string; key?: string; altKey: boolean },
+ mac: boolean,
+): string {
+ const key = event.key ?? "";
+ if ((mac && event.altKey) || key.length !== 1) return "";
+ if (event.code.startsWith("Numpad")) return "";
+ const typed = keyToCode(key);
+ return typed === key || /^(Key|Digit)/.test(typed) ? "" : typed;
+}
+
+/** The Latin letter a key types, as the browser's own Ctrl+C reads it (Dvorak, #14035), else the
+ * punctuation a letter position types, else the position. Option on macOS rewrites the key. */
+function eventCode(
+ event: { code: string; key?: string; altKey: boolean },
+ mac: boolean,
+): string {
+ const code = event.code || keyToCode(event.key ?? "");
+ const key = event.key ?? "";
+ if ((mac && event.altKey) || key.length !== 1 || code.startsWith("Numpad")) {
+ return code;
+ }
+ if (/^[a-z]$/i.test(key)) return `Key${key.toUpperCase()}`;
+ if (/^Key[A-Z]$/.test(code)) return typedPunctuation(event, mac) || code;
+ return code;
+}
+
export function matchesBinding(
binding: ShortcutBinding,
event: {
@@ -554,8 +580,13 @@ export function matchesBinding(
},
mac = isMacPlatform(),
): boolean {
- const code = event.code || keyToCode(event.key ?? "");
- if (code !== binding.code) return false;
+ // Also the punctuation typed (Dvorak's / sits on [). Not recorded: German types / with Shift.
+ if (
+ eventCode(event, mac) !== binding.code &&
+ typedPunctuation(event, mac) !== binding.code
+ ) {
+ return false;
+ }
// Off macOS a Ctrl chord is unreachable, and without this a value stored on
// a Mac would fall through the checks below and fire on the bare key.
if (!mac && binding.ctrl) return false;
diff --git a/studio/frontend/tests/keyboard-shortcuts.test.ts b/studio/frontend/tests/keyboard-shortcuts.test.ts
index 33eb01cf3f1..b6f3bb72f9e 100644
--- a/studio/frontend/tests/keyboard-shortcuts.test.ts
+++ b/studio/frontend/tests/keyboard-shortcuts.test.ts
@@ -50,6 +50,7 @@ function keyEvent(
altKey: boolean;
/** What a Windows or Linux layout reports for AltGr, alongside Ctrl+Alt. */
altGraph: boolean;
+ key: string;
}> = {},
) {
const { altGraph = false, ...rest } = mods;
@@ -668,6 +669,127 @@ test("labels use each platform's own modifier notation", () => {
assert.equal(formatBindingLabel(comma, true), "⌘,");
});
+test("a key typing a letter answers as that letter, whatever its position", () => {
+ const sidebar = parseBinding("Mod+KeyB");
+ assert.ok(sidebar);
+ // Dvorak types x on the B position: Ctrl+X there is Cut (#14035).
+ const dvorakX = keyEvent("KeyB", { ctrlKey: true, key: "x" });
+ assert.equal(matchesBinding(sidebar, dvorakX, false), false);
+ assert.equal(formatBindingValue(bindingFromEvent(dvorakX, false)!), "Mod+KeyX");
+ const dvorakB = keyEvent("KeyN", { ctrlKey: true, key: "b" });
+ assert.equal(matchesBinding(sidebar, dvorakB, false), true);
+ const recorded = bindingFromEvent(dvorakB, false);
+ assert.ok(recorded);
+ assert.equal(formatBindingValue(recorded), "Mod+KeyB");
+ assert.equal(formatBindingLabel(recorded, false), "Ctrl+B");
+ const shifted = parseBinding("Mod+Shift+KeyO");
+ assert.ok(shifted);
+ assert.equal(
+ matchesBinding(shifted, keyEvent("KeyS", { metaKey: true, shiftKey: true, key: "O" }), true),
+ true,
+ );
+ const slash = parseBinding("Mod+Slash");
+ assert.ok(slash);
+ assert.equal(
+ matchesBinding(slash, keyEvent("Slash", { ctrlKey: true, key: "z" }), false),
+ false,
+ );
+});
+
+test("a letter position typing punctuation answers as that punctuation", () => {
+ const settings = parseBinding("Mod+Comma");
+ assert.ok(settings);
+ assert.equal(
+ matchesBinding(settings, keyEvent("KeyW", { metaKey: true, key: "," }), true),
+ true,
+ );
+ assert.equal(
+ matchesBinding(settings, keyEvent("Comma", { metaKey: true, key: "w" }), true),
+ false,
+ );
+});
+
+test("a punctuation position also answers to the punctuation it types", () => {
+ const slash = parseBinding("Mod+Slash");
+ assert.ok(slash);
+ const dvorakSlash = keyEvent("BracketLeft", { ctrlKey: true, key: "/" });
+ assert.equal(matchesBinding(slash, dvorakSlash, false), true);
+ assert.equal(matchesBinding(slash, keyEvent("BracketLeft", { metaKey: true, key: "/" }), true), true);
+ assert.equal(formatBindingValue(bindingFromEvent(dvorakSlash, false)!), "Mod+BracketLeft");
+ assert.equal(
+ matchesBinding(slash, keyEvent("BracketLeft", { metaKey: true, altKey: true, key: "/" }), true),
+ false,
+ );
+});
+
+test("keys that type no letter keep their position", () => {
+ // German types / only with Shift, so Mod+Slash stays on the position.
+ const slash = parseBinding("Mod+Slash");
+ assert.ok(slash);
+ assert.equal(
+ matchesBinding(slash, keyEvent("Slash", { ctrlKey: true, key: "-" }), false),
+ true,
+ );
+ const sidebar = parseBinding("Mod+KeyB");
+ assert.ok(sidebar);
+ assert.equal(
+ matchesBinding(sidebar, keyEvent("KeyB", { ctrlKey: true, key: "и" }), false),
+ true,
+ );
+ const archive = parseBinding("Mod+Alt+KeyE");
+ assert.ok(archive);
+ assert.equal(
+ matchesBinding(archive, keyEvent("KeyE", { metaKey: true, altKey: true, key: "Dead" }), true),
+ true,
+ );
+ const bookmarks = parseBinding("Mod+Alt+KeyB");
+ assert.ok(bookmarks);
+ assert.equal(
+ matchesBinding(bookmarks, keyEvent("KeyB", { metaKey: true, altKey: true, key: "∫" }), true),
+ true,
+ );
+ assert.equal(
+ matchesBinding(archive, keyEvent("KeyD", { ctrlKey: true, altKey: true, key: "e" }), false),
+ true,
+ );
+ assert.equal(
+ matchesBinding(archive, keyEvent("KeyE", { ctrlKey: true, altKey: true, altGraph: true, key: "€" }), false),
+ false,
+ );
+ const pad = parseBinding("Mod+Numpad1");
+ assert.ok(pad);
+ assert.equal(
+ matchesBinding(pad, keyEvent("Numpad1", { ctrlKey: true, key: "1" }), false),
+ true,
+ );
+});
+
+test("on US QWERTY every key answers to its own position", () => {
+ const us: Record<string, [string, string]> = {
+ Comma: [",", "<"], Period: [".", ">"], Slash: ["/", "?"], Semicolon: [";", ":"],
+ Quote: ["'", "\""], BracketLeft: ["[", "{"], BracketRight: ["]", "}"],
+ Backslash: ["\\", "|"], Minus: ["-", "_"], Equal: ["=", "+"], Backquote: ["`", "~"],
+ };
+ for (let i = 0; i < 26; i++) {
+ const letter = String.fromCharCode(97 + i);
+ us[`Key${letter.toUpperCase()}`] = [letter, letter.toUpperCase()];
+ }
+ const shifted = "!@#$%^&*()";
+ for (let i = 0; i < 10; i++) us[`Digit${(i + 1) % 10}`] = [`${(i + 1) % 10}`, shifted[i]];
+ for (const [code, [plain, withShift]] of Object.entries(us)) {
+ for (const mac of [false, true]) {
+ for (const shiftKey of [false, true]) {
+ const event = keyEvent(code, {
+ ...(mac ? { metaKey: true } : { ctrlKey: true }),
+ shiftKey,
+ key: shiftKey ? withShift : plain,
+ });
+ assert.equal(bindingFromEvent(event, mac)?.code, code, `${code} shift=${shiftKey}`);
+ }
+ }
+ }
+});
+
test("an override wins, and null means unassigned", () => {
assert.equal(resolveBinding({}, "toggleSidebar"), "Mod+KeyB");
assert.equal(