Skip to content

Commit

Permalink
fix(useKeyboardShortcuts): allow for shift key plus special characters
Browse files Browse the repository at this point in the history
Support characters like '?' and uppercase letters for keyboard shortcuts.
  • Loading branch information
PaulHax committed Jul 26, 2024
1 parent 72bf66b commit b14c519
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/composables/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
20 changes: 18 additions & 2 deletions src/composables/useKeyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export const LAYER_PRESET_BY_MODALITY: Record<string, string> = {
};
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',
Expand Down

0 comments on commit b14c519

Please sign in to comment.