diff --git a/src/components/SearchBar.vue b/src/components/SearchBar.vue index 62842358..d54f3181 100644 --- a/src/components/SearchBar.vue +++ b/src/components/SearchBar.vue @@ -237,15 +237,14 @@ export default { const shortcuts = [ { - shortcut: ['KeyK', this.modKey], + shortcut: 'KeyK', callback: ifClose(() => this.open()), }, - { shortcut: ['Escape'], callback: ifOpen(() => this.close()) }, ]; for (const i in searchGroups) { shortcuts.push({ - shortcut: [this.modKey, `Digit${Number(i) + 1}`], + shortcut: `Digit${Number(i) + 1}`, callback: ifOpen(() => { const group = searchGroups[i]; const value = this.searcher.filters.groupFilters[group]; @@ -262,12 +261,20 @@ export default { }, setShortcuts() { for (const { shortcut, callback } of this.getShortcuts()) { - this.shortcuts.meta.set(shortcut, callback); + if (this.platform === 'Mac') { + this.shortcuts.meta.set([shortcut], callback); + } else { + this.shortcuts.ctrl.set([shortcut], callback); + } } }, deleteShortcuts() { for (const { shortcut } of this.getShortcuts()) { - this.shortcuts.meta.delete(shortcut); + if (this.platform === 'Mac') { + this.shortcuts.meta.delete([shortcut]); + } else { + this.shortcuts.ctrl.delete([shortcut]); + } } }, modKeyText(key) { @@ -305,7 +312,7 @@ export default { }, select(idx) { this.idx = idx ?? this.idx; - this.suggestions[this.idx]?.action(); + this.suggestionsthis.idx?.action(); this.close(); }, scrollToHighlighted() { diff --git a/src/utils/vueUtils.ts b/src/utils/vueUtils.ts index 2ad3281f..cdeca992 100644 --- a/src/utils/vueUtils.ts +++ b/src/utils/vueUtils.ts @@ -1,4 +1,4 @@ -import { onMounted, onUnmounted, reactive, ref, unref, watch } from 'vue'; +import { onMounted, onUnmounted, reactive, ref, watch } from 'vue'; interface ModMap { alt: boolean; @@ -114,6 +114,7 @@ export class Shortcuts { } export function useKeys() { + const isMac = navigator.userAgent.indexOf('Mac') !== -1; const keys: Keys = reactive({ pressed: new Set(), alt: false, @@ -124,21 +125,32 @@ export function useKeys() { }); const keydownListener = (e: KeyboardEvent) => { - keys.pressed.add(e.code); keys.alt = e.altKey; keys.ctrl = e.ctrlKey; keys.meta = e.metaKey; keys.shift = e.shiftKey; keys.repeat = e.repeat; + + const { code } = e; + if ( + code.startsWith('Alt') || + code.startsWith('Ctrl') || + code.startsWith('Meta') || + code.startsWith('Shift') + ) { + return; + } + + keys.pressed.add(code); }; const keyupListener = (e: KeyboardEvent) => { - keys.pressed.delete(e.code); - - // Key up won't trigger on macOS for other keys. - if (e.code === 'MetaLeft') { - keys.pressed.clear(); + const { code } = e; + if (code.startsWith('Meta') && isMac) { + return keys.pressed.clear(); } + + keys.pressed.delete(code); }; onMounted(() => {