Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

hotkey: Fix Cmd+<shifted_symbol> shortcuts on macOS.

Aman Agrawal7a5a1effd766
diff --git a/web/src/hotkey.ts b/web/src/hotkey.ts
index 23546bdc320..4116fcda50d 100644
--- a/web/src/hotkey.ts
+++ b/web/src/hotkey.ts
@@ -345,8 +345,15 @@ export function get_keydown_hotkey(e: JQuery.KeyDownEvent): Hotkey | Hotkey[] |
// the same physical key combination that a QWERTY user would
// use (e.g. 'ф' on Cyrillic maps to 'a' on QWERTY). In such cases,
// we derive the QWERTY equivalent using `e.code` and the `CODE_TO_QWERTY_CHAR` map.
+ //
+ // The same `e.code` path is needed on macOS when Cmd+Shift are
+ // both held, as browsers report the unshifted key in that case
+ // (e.g. Cmd+Shift+2 gives e.key="2" rather than "@"). Without
+ // this, `Cmd+<shifted_symbol>` hotkeys would never match.
+ const mac_cmd_shift = common.has_mac_keyboard() && e.metaKey && e.shiftKey;
const use_event_key =
- common.is_printable_ascii(e.key) || KNOWN_NAMED_KEY_ATTRIBUTE_VALUES.has(e.key);
+ KNOWN_NAMED_KEY_ATTRIBUTE_VALUES.has(e.key) ||
+ (common.is_printable_ascii(e.key) && !mac_cmd_shift);
let key = e.key;
if (!use_event_key) {
const code = `${e.shiftKey ? "Shift+" : ""}${e.code}`;
diff --git a/web/tests/hotkey.test.cjs b/web/tests/hotkey.test.cjs
index 505414a1c00..e19651a57ad 100644
--- a/web/tests/hotkey.test.cjs
+++ b/web/tests/hotkey.test.cjs
@@ -243,7 +243,18 @@ run_test("mappings", () => {
assert.equal(map_down("c", false, true, false), undefined);
assert.equal(map_down("k", false, false, true).name, "search_with_k");
assert.equal(map_down("k", false, true, false), undefined);
- assert.equal(map_down("@", true, false, true).name, "open_mentions_view");
+ // On macOS, browsers report the unshifted key when Cmd+Shift
+ // are both held (e.g. Cmd+Shift+2 gives e.key="2", not "@"),
+ // so we fall back to `e.code` to recover the intended symbol.
+ assert.equal(
+ hotkey.get_keydown_hotkey({
+ key: "2",
+ code: "Digit2",
+ shiftKey: true,
+ metaKey: true,
+ }).name,
+ "open_mentions_view",
+ );
assert.equal(map_down("@", true, true, false), undefined);
assert.equal(map_down("s", false, false, true).name, "star_message");
assert.equal(map_down("s", false, true, false), undefined);