fix: keybinding modifier issue (#3163)

This commit is contained in:
Anwarul Islam
2023-07-18 00:26:08 +06:00
committed by GitHub
parent 9402bb9285
commit 51efb35aa6

View File

@@ -14,7 +14,13 @@ let keybindingsEnabled = true
* Alt is also regarded as macOS OPTION (⌥) key * 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!) * 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 */ /* eslint-disable prettier/prettier */
// prettier-ignore // prettier-ignore
@@ -143,18 +149,19 @@ function getPressedKey(ev: KeyboardEvent): Key | null {
} }
function getActiveModifier(ev: KeyboardEvent): ModifierKeys | 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) // active modifier: ctrl | alt | ctrl-alt | ctrl-shift | ctrl-alt-shift | alt-shift
// Control key (+ Command) gets priority and if Alt is also pressed, it is ignored // modiferKeys object's keys are sorted to match the above order
if (isAppleDevice() && ev.metaKey) return isShiftKey ? "ctrl-shift" : "ctrl" const activeModifier = Object.keys(modifierKeys)
else if (!isAppleDevice() && ev.ctrlKey) .filter((key) => modifierKeys[key as keyof typeof modifierKeys])
return isShiftKey ? "ctrl-shift" : "ctrl" .join("-")
// Test for Alt key return activeModifier === "" ? null : (activeModifier as ModifierKeys)
if (ev.altKey) return isShiftKey ? "alt-shift" : "alt"
return null
} }
/** /**