Skip to content

Commit

Permalink
fix: keybinding modifier issue (hoppscotch#3163)
Browse files Browse the repository at this point in the history
  • Loading branch information
anwarulislam authored Jul 17, 2023
1 parent 9402bb9 commit 51efb35
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/hoppscotch-common/src/helpers/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,18 +149,19 @@ function getPressedKey(ev: KeyboardEvent): Key | null {
}

function getActiveModifier(ev: KeyboardEvent): ModifierKeys | null {
const isShiftKey = 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"
const modifierKeys = {
ctrl: isAppleDevice() ? ev.metaKey : ev.ctrlKey,
alt: ev.altKey,
shift: ev.shiftKey,
}

// Test for Alt key
if (ev.altKey) return isShiftKey ? "alt-shift" : "alt"
// 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("-")

return null
return activeModifier === "" ? null : (activeModifier as ModifierKeys)
}

/**
Expand Down

0 comments on commit 51efb35

Please sign in to comment.