From 51efb35aa6eef01ad20a973a55333c92ceb795fb Mon Sep 17 00:00:00 2001 From: Anwarul Islam Date: Tue, 18 Jul 2023 00:26:08 +0600 Subject: [PATCH] fix: keybinding modifier issue (#3163) --- .../src/helpers/keybindings.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/keybindings.ts b/packages/hoppscotch-common/src/helpers/keybindings.ts index f2f62f128..0f703ebb7 100644 --- a/packages/hoppscotch-common/src/helpers/keybindings.ts +++ b/packages/hoppscotch-common/src/helpers/keybindings.ts @@ -14,7 +14,13 @@ let keybindingsEnabled = true * Alt is also regarded as macOS OPTION (⌥) key * Ctrl is also regarded as macOS COMMAND (⌘) key (NOTE: this differs from HTML Keyboard spec where COMMAND is Meta key!) */ -type ModifierKeys = "ctrl" | "alt" | "ctrl-shift" | "alt-shift" +type ModifierKeys = + | "ctrl" + | "alt" + | "ctrl-shift" + | "alt-shift" + | "ctrl-alt" + | "ctrl-alt-shift" /* eslint-disable prettier/prettier */ // prettier-ignore @@ -143,18 +149,19 @@ function getPressedKey(ev: KeyboardEvent): Key | null { } function getActiveModifier(ev: KeyboardEvent): ModifierKeys | null { - const isShiftKey = ev.shiftKey + const modifierKeys = { + ctrl: isAppleDevice() ? ev.metaKey : ev.ctrlKey, + alt: ev.altKey, + shift: ev.shiftKey, + } - // We only allow one modifier key to be pressed (for now) - // Control key (+ Command) gets priority and if Alt is also pressed, it is ignored - if (isAppleDevice() && ev.metaKey) return isShiftKey ? "ctrl-shift" : "ctrl" - else if (!isAppleDevice() && ev.ctrlKey) - return isShiftKey ? "ctrl-shift" : "ctrl" + // active modifier: ctrl | alt | ctrl-alt | ctrl-shift | ctrl-alt-shift | alt-shift + // modiferKeys object's keys are sorted to match the above order + const activeModifier = Object.keys(modifierKeys) + .filter((key) => modifierKeys[key as keyof typeof modifierKeys]) + .join("-") - // Test for Alt key - if (ev.altKey) return isShiftKey ? "alt-shift" : "alt" - - return null + return activeModifier === "" ? null : (activeModifier as ModifierKeys) } /**