Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
hotkey: Support non-latin keyboard locales for shortcuts.
Prakhar Pratyushb837c47ac387
diff --git a/web/src/hotkey.js b/web/src/hotkey.jsindex 82311f71887..1bcff0369d6 100644--- a/web/src/hotkey.js+++ b/web/src/hotkey.js@@ -217,10 +217,124 @@ const KEYDOWN_MAPPINGS = {"Shift+Delete": {name: "delete", message_view_only: false},};+function is_printable_ascii(key) {+ // ASCII printable characters (character code 32-126) -> " " to "~".+ // It includes letters, digits, punctuation marks, and a few+ // miscellaneous symbols.+ return key.length === 1 && key >= " " && key <= "~";+}++const KNOWN_NAMED_KEY_ATTRIBUTE_VALUES = new Set([+ "Alt",+ "ArrowDown",+ "ArrowLeft",+ "ArrowRight",+ "ArrowUp",+ "Backspace",+ "Control",+ "Delete",+ "End",+ "Enter",+ "Escape",+ "Home",+ "Meta",+ "PageDown",+ "PageUp",+ "Shift",+ "Tab",+]);++const CODE_TO_QWERTY_CHAR = {+ KeyA: "a",+ KeyB: "b",+ KeyC: "c",+ KeyD: "d",+ KeyE: "e",+ KeyF: "f",+ KeyG: "g",+ KeyH: "h",+ KeyI: "i",+ KeyJ: "j",+ KeyK: "k",+ KeyL: "l",+ KeyM: "m",+ KeyN: "n",+ KeyO: "o",+ KeyP: "p",+ KeyQ: "q",+ KeyR: "r",+ KeyS: "s",+ KeyT: "t",+ KeyU: "u",+ KeyV: "v",+ KeyW: "w",+ KeyX: "x",+ KeyY: "y",+ KeyZ: "z",+ "Shift+KeyA": "A",+ "Shift+KeyB": "B",+ "Shift+KeyC": "C",+ "Shift+KeyD": "D",+ "Shift+KeyE": "E",+ "Shift+KeyF": "F",+ "Shift+KeyG": "G",+ "Shift+KeyH": "H",+ "Shift+KeyI": "I",+ "Shift+KeyJ": "J",+ "Shift+KeyK": "K",+ "Shift+KeyL": "L",+ "Shift+KeyM": "M",+ "Shift+KeyN": "N",+ "Shift+KeyO": "O",+ "Shift+KeyP": "P",+ "Shift+KeyQ": "Q",+ "Shift+KeyR": "R",+ "Shift+KeyS": "S",+ "Shift+KeyT": "T",+ "Shift+KeyU": "U",+ "Shift+KeyV": "V",+ "Shift+KeyW": "W",+ "Shift+KeyX": "X",+ "Shift+KeyY": "Y",+ "Shift+KeyZ": "Z",+ BracketLeft: "[",+ Equal: "=",+ Minus: "-",+ Period: ".",+ Quote: "'",+ "Shift+Comma": "<",+ "Shift+Digit2": "@",+ "Shift+Digit8": "*",+ "Shift+Equal": "+",+ "Shift+Period": ">",+ "Shift+Semicolon": ":",+ "Shift+Slash": "?",+ Slash: "/",+ Space: " ",+};+// Keyboard Event Viewer tool:// https://w3c.github.io/uievents/tools/key-event-viewer.htmlexport function get_keydown_hotkey(e) {- if (common.has_mac_keyboard() && e.ctrlKey && e.key !== "[") {+ // Determine the key pressed in a consistent way.+ //+ // For keyboard layouts (e.g. QWERTY) where `e.key` results+ // in printable ASCII characters or named key attribute values+ // like "Tab", "Enter", etc., we use `e.key` directly.+ //+ // However, for layouts where the character defined for a hotkey+ // can't be typed directly (e.g. 'a' in Cyrillic), users press+ // 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.+ const use_event_key = is_printable_ascii(e.key) || KNOWN_NAMED_KEY_ATTRIBUTE_VALUES.has(e.key);+ let key = e.key;+ if (!use_event_key) {+ const code = `${e.shiftKey ? "Shift+" : ""}${e.code}`;+ key = CODE_TO_QWERTY_CHAR[code];+ }++ if (common.has_mac_keyboard() && e.ctrlKey && key !== "[") {// On macOS, Cmd is used instead of Ctrl. Except 'Ctrl + ['.return undefined;}@@ -231,7 +345,9 @@ export function get_keydown_hotkey(e) {// Make lowercase a-z uppercase; leave others unchanged.// We distinguish between 'a' and 'A' as 'A' vs 'Shift+A'.// This helps to avoid Caps Lock affecting shortcuts.- let key = /^[a-z]$/.test(e.key) ? e.key.toUpperCase() : e.key;+ if (/^[a-z]$/.test(key)) {+ key = key.toUpperCase();+ }if (e.shiftKey && !KNOWN_IGNORE_SHIFT_MODIFIER_KEYS.has(key)) {key = "Shift+" + key;diff --git a/web/tests/hotkey.test.cjs b/web/tests/hotkey.test.cjsindex cf829c2bb95..acdca24bd00 100644--- a/web/tests/hotkey.test.cjs+++ b/web/tests/hotkey.test.cjs@@ -244,6 +244,78 @@ run_test("mappings", () => {assert.equal(map_down("P", false, false, false, true).name, "toggle_compose_preview");});+run_test("mappings non-latin keyboard", () => {+ // This test replicates the logic of the "mappings" test above+ // but uses a non-Latin (Russian) keyboard layout to verify that+ // hotkeys work irrespective of the keyboard layout.+ // Layout used: https://kbdlayout.info/kbdru/overview+virtualkeys?arrangement=ANSI104+ function map_down(key, code, shiftKey, ctrlKey, metaKey, altKey) {+ return hotkey.get_keydown_hotkey({+ key,+ code,+ shiftKey,+ ctrlKey,+ metaKey,+ altKey,+ });+ }++ // Test mappings.+ assert.equal(map_down("Р", "KeyH", true).name, "view_edit_history");+ assert.equal(map_down("Т", "KeyN", true).name, "narrow_to_next_unread_followed_topic");+ assert.deepEqual(+ map_down("М", "KeyV", true).map((item) => item.name),+ ["view_selected_stream", "toggle_read_receipts"],+ );+ assert.equal(map_down("о", "KeyJ").name, "vim_down");+ assert.equal(map_down("х", "BracketLeft", false, true).name, "escape");+ assert.equal(map_down("с", "KeyC", false, true).name, "copy_with_c");+ assert.equal(map_down("л", "KeyK", false, true).name, "search_with_k");+ assert.equal(map_down("ы", "KeyS", false, true).name, "star_message");+ assert.equal(map_down("з", "KeyP", false, false, false, true).name, "toggle_compose_preview");++ // More negative tests.+ assert.equal(map_down("м", "KeyV", false, true), undefined);+ assert.equal(map_down("я", "KeyZ", false, true), undefined);+ assert.equal(map_down("е", "KeyT", false, true), undefined);+ assert.equal(map_down("к", "KeyR", false, true), undefined);+ assert.equal(map_down("щ", "KeyO", false, true), undefined);+ assert.equal(map_down("з", "KeyP", false, true), undefined);+ assert.equal(map_down("ф", "KeyA", false, true), undefined);+ assert.equal(map_down("а", "KeyF", false, true), undefined);+ assert.equal(map_down("р", "KeyH", false, true), undefined);+ assert.equal(map_down("ч", "KeyX", false, true), undefined);+ assert.equal(map_down("т", "KeyN", false, true), undefined);+ assert.equal(map_down("ь", "KeyM", false, true), undefined);+ assert.equal(map_down("с", "KeyC", false, false, true), undefined);+ assert.equal(map_down("л", "KeyK", false, false, true), undefined);+ assert.equal(map_down("ы", "KeyS", false, false, true), undefined);+ assert.equal(map_down("Л", "KeyK", true, true), undefined);+ assert.equal(map_down("Ы", "KeyS", true, true), undefined);+ assert.equal(map_down("Х", "BracketLeft", true, true, false), undefined);+ assert.equal(map_down("З", "KeyP", true, false, false, true), undefined);++ // Cmd tests for MacOS+ navigator.platform = "MacIntel";+ assert.equal(map_down("х", "BracketLeft", false, true, false).name, "escape");+ assert.equal(map_down("х", "BracketLeft", false, false, true), undefined);+ assert.equal(map_down("с", "KeyC", false, false, true).name, "copy_with_c");+ assert.equal(map_down("с", "KeyC", false, true, true), undefined);+ assert.equal(map_down("с", "KeyC", false, true, false), undefined);+ assert.equal(map_down("л", "KeyK", false, false, true).name, "search_with_k");+ assert.equal(map_down("л", "KeyK", false, true, false), undefined);+ assert.equal(map_down("ы", "KeyS", false, false, true).name, "star_message");+ assert.equal(map_down("ы", "KeyS", false, true, false), undefined);+ // Reset platform+ navigator.platform = "";++ // Caps Lock doesn't interfere with shortcuts.+ assert.equal(map_down("Ф", "KeyA").name, "open_combined_feed");+ assert.equal(map_down("Ф", "KeyA", true).name, "stream_cycle_backward");+ assert.equal(map_down("С", "KeyC", false, true).name, "copy_with_c");+ assert.equal(map_down("З", "KeyP", false, false, false, true).name, "toggle_compose_preview");+});+function process(s, shiftKey) {const e = {key: s,