From b14c519976867a07eb9deb5fb92a080deba295d7 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Fri, 26 Jul 2024 11:33:14 -0400 Subject: [PATCH] fix(useKeyboardShortcuts): allow for shift key plus special characters Support characters like '?' and uppercase letters for keyboard shortcuts. --- src/composables/actions.ts | 2 +- src/composables/useKeyboardShortcuts.ts | 20 ++++++++++++++++++-- src/config.ts | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/composables/actions.ts b/src/composables/actions.ts index 99660b60e..beb0a9a3d 100644 --- a/src/composables/actions.ts +++ b/src/composables/actions.ts @@ -33,7 +33,7 @@ const setTool = (tool: Tools) => () => { const showKeyboardShortcuts = () => { const keyboardStore = useKeyboardShortcutsStore(); - keyboardStore.settingsOpen = true; + keyboardStore.settingsOpen = !keyboardStore.settingsOpen; }; export const ACTION_TO_FUNC = { diff --git a/src/composables/useKeyboardShortcuts.ts b/src/composables/useKeyboardShortcuts.ts index 6acd1d9b7..1d600afc5 100644 --- a/src/composables/useKeyboardShortcuts.ts +++ b/src/composables/useKeyboardShortcuts.ts @@ -17,9 +17,25 @@ export function useKeyboardShortcuts() { unwatchFuncs.forEach((unwatch) => unwatch()); unwatchFuncs = getEntries(actionMap).map(([action, key]) => { + const individualKeys = key.split('+'); + const lastKey = individualKeys[individualKeys.length - 1]; + return whenever(keys[key], () => { - // basic detection for exact modifier match - if (keys.current.size === key.split('+').length) { + const shiftPressed = keys.current.has('shift'); + const lastPressedKey = Array.from(keys.current).pop(); + const currentKeyWithCase = shiftPressed + ? lastPressedKey?.toUpperCase() ?? lastPressedKey + : lastPressedKey; + + // keyCountMatches checks for exact modifier match + const keyCountMatches = keys.current.size === individualKeys.length; + const lastKeyMatches = lastKey === currentKeyWithCase; + const shiftCaseMatches = + shiftPressed && + keys.current.size - 1 === individualKeys.length && + lastKeyMatches; + + if ((keyCountMatches && lastKeyMatches) || shiftCaseMatches) { ACTION_TO_FUNC[action](); } }); diff --git a/src/config.ts b/src/config.ts index 03ffcff0d..5c4375c68 100644 --- a/src/config.ts +++ b/src/config.ts @@ -285,7 +285,7 @@ export const LAYER_PRESET_BY_MODALITY: Record = { }; export const LAYER_PRESET_DEFAULT = 'Blue to Red Rainbow'; -// Keyboard shortcuts/hotkeys. Can add modifiers: 'Shift+Ctrl+A' +// Keyboard shortcuts/hotkeys. Can add modifiers: 'Shift+Ctrl+a' export const ACTION_TO_KEY = { windowLevel: 'l', pan: 'n',