Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix keyboard shortcuts normalization (#8576)

Shahmir Varqha93b330ec212e
diff --git a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
index 3cd7b88a64..c3b4b4c3b6 100644
--- a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
+++ b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
@@ -174,7 +174,9 @@ export const KeyboardShortcuts: React.FC = () => {
return;
}
- let key = e.key.toLowerCase();
+ // Single character keys are always lowercase (e.g. "a", "b", "c")
+ // But we should preserve the original case for other keys (e.g. "Enter", "Escape")
+ let key = e.key.length === 1 ? e.key.toLowerCase() : e.key;
// Handle edge cases
if (e.key === " ") {
key = "Space";
diff --git a/frontend/src/core/hotkeys/__tests__/hotkeys.test.ts b/frontend/src/core/hotkeys/__tests__/hotkeys.test.ts
index a314ba80f9..591af46631 100644
--- a/frontend/src/core/hotkeys/__tests__/hotkeys.test.ts
+++ b/frontend/src/core/hotkeys/__tests__/hotkeys.test.ts
@@ -1,6 +1,12 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
-import { type Hotkey, type HotkeyAction, HotkeyProvider } from "../hotkeys";
+import {
+ type Hotkey,
+ type HotkeyAction,
+ HotkeyProvider,
+ normalizeKeyString,
+ OverridingHotkeyProvider,
+} from "../hotkeys";
/**
* Just a helper.
@@ -72,3 +78,60 @@ describe("HotkeyProvider platform separation", () => {
expect(linux.getHotkey("cell.format").key).toBe("Ctrl-Shift-L");
});
});
+
+describe("normalizeKeyString", () => {
+ it("should capitalize multi-character base key names", () => {
+ expect(normalizeKeyString("Shift-enter")).toBe("Shift-Enter");
+ expect(normalizeKeyString("Cmd-enter")).toBe("Cmd-Enter");
+ expect(normalizeKeyString("Ctrl-backspace")).toBe("Ctrl-Backspace");
+ expect(normalizeKeyString("Alt-tab")).toBe("Alt-Tab");
+ expect(normalizeKeyString("Cmd-Shift-arrowUp")).toBe("Cmd-Shift-ArrowUp");
+ });
+
+ it("should leave already-correct key names unchanged", () => {
+ expect(normalizeKeyString("Shift-Enter")).toBe("Shift-Enter");
+ expect(normalizeKeyString("Cmd-Enter")).toBe("Cmd-Enter");
+ expect(normalizeKeyString("Mod-Shift-Enter")).toBe("Mod-Shift-Enter");
+ });
+
+ it("should leave single-character keys unchanged", () => {
+ expect(normalizeKeyString("Cmd-a")).toBe("Cmd-a");
+ expect(normalizeKeyString("Ctrl-Shift-z")).toBe("Ctrl-Shift-z");
+ expect(normalizeKeyString("a")).toBe("a");
+ });
+
+ it("should handle keys without modifiers", () => {
+ expect(normalizeKeyString("enter")).toBe("Enter");
+ expect(normalizeKeyString("Escape")).toBe("Escape");
+ expect(normalizeKeyString("F12")).toBe("F12");
+ });
+});
+
+describe("OverridingHotkeyProvider", () => {
+ it("should normalize lowercase key overrides", () => {
+ const provider = new OverridingHotkeyProvider(
+ {
+ "cell.run": "Shift-enter",
+ "cell.runAndNewBelow": "Cmd-enter",
+ },
+ { platform: "mac" },
+ );
+
+ expect(provider.getHotkey("cell.run").key).toBe("Shift-Enter");
+ expect(provider.getHotkey("cell.runAndNewBelow").key).toBe("Cmd-Enter");
+ });
+
+ it("should return defaults when no override is set", () => {
+ const provider = new OverridingHotkeyProvider({}, { platform: "mac" });
+ expect(provider.getHotkey("cell.run").key).toBe("Cmd-Enter");
+ expect(provider.getHotkey("cell.runAndNewBelow").key).toBe("Shift-Enter");
+ });
+
+ it("should pass through correctly-cased overrides unchanged", () => {
+ const provider = new OverridingHotkeyProvider(
+ { "cell.run": "Shift-Enter" },
+ { platform: "mac" },
+ );
+ expect(provider.getHotkey("cell.run").key).toBe("Shift-Enter");
+ });
+});
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 2d2503ff07..0d2b2ba853 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -535,10 +535,26 @@ export class OverridingHotkeyProvider extends HotkeyProvider {
override getHotkey(action: HotkeyAction): ResolvedHotkey {
const base = super.getHotkey(action);
- const key = this.overrides[action] || base.key;
+ const override = this.overrides[action];
return {
name: base.name,
- key,
+ key: override ? normalizeKeyString(override) : base.key,
};
}
}
+
+const MODIFIER_RE = /^(cmd|ctrl|alt|shift|meta|mod)$/i;
+
+/**
+ * Capitalize multi-character base key names so they match the
+ * casing that KeyboardEvent.key (and therefore CodeMirror) uses.
+ * e.g. "Shift-enter" → "Shift-Enter", "Cmd-backspace" → "Cmd-Backspace"
+ */
+export function normalizeKeyString(key: string): string {
+ const parts = key.split("-");
+ const last = parts[parts.length - 1];
+ if (last.length > 1 && !MODIFIER_RE.test(last)) {
+ parts[parts.length - 1] = last.charAt(0).toUpperCase() + last.slice(1);
+ }
+ return parts.join("-");
+}