From c067d93a743937e02ceae4291dff789bd457868d Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Thu, 12 Nov 2020 00:05:03 +1100 Subject: [PATCH] #220 stricter keyboard shortcut listeners --- mods/core/client.js | 32 +++++++++++-- mods/core/enhancerMenu.js | 8 +++- mods/core/render.js | 99 +++++++++++++++++++++++---------------- 3 files changed, 94 insertions(+), 45 deletions(-) diff --git a/mods/core/client.js b/mods/core/client.js index 7b5b082..474fae9 100644 --- a/mods/core/client.js +++ b/mods/core/client.js @@ -23,7 +23,13 @@ module.exports = (store, __exports) => { if (event.key === 'F5') location.reload(); // open menu on hotkey toggle if (store().menu_toggle) { - const hotkey = toKeyEvent(store().menu_toggle); + const hotkey = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(store().menu_toggle), + }; let triggered = true; for (let prop in hotkey) if ( @@ -37,7 +43,13 @@ module.exports = (store, __exports) => { const tabStore = () => store('e1692c29-475e-437b-b7ff-3eee872e1a42'); if (tabStore().select_modifier) { // switch between tabs via key modifier - const select_tab_modifier = toKeyEvent(select_tab_modifier); + const select_tab_modifier = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().select_modifier), + }; let triggered = true; for (let prop in select_tab_modifier) if (select_tab_modifier[prop] !== event[prop]) triggered = false; @@ -61,14 +73,26 @@ module.exports = (store, __exports) => { } if (tabStore().new_tab) { // create/close tab keybindings - const new_tab_keybinding = toKeyEvent(tabStore().new_tab); + const new_tab_keybinding = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().new_tab), + }; let triggered = true; for (let prop in new_tab_keybinding) if (new_tab_keybinding[prop] !== event[prop]) triggered = false; if (triggered) electron.ipcRenderer.sendToHost('enhancer:new-tab'); } if (tabStore().close_tab) { - const close_tab_keybinding = toKeyEvent(tabStore().close_tab); + const close_tab_keybinding = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().close_tab), + }; let triggered = true; for (let prop in close_tab_keybinding) if (close_tab_keybinding[prop] !== event[prop]) triggered = false; diff --git a/mods/core/enhancerMenu.js b/mods/core/enhancerMenu.js index 7e59c47..795224b 100644 --- a/mods/core/enhancerMenu.js +++ b/mods/core/enhancerMenu.js @@ -129,7 +129,13 @@ window['__start'] = async () => { $popup.classList.remove('visible'); // close window on hotkey toggle if (coreStore().menu_toggle) { - const hotkey = toKeyEvent(coreStore().menu_toggle); + const hotkey = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(coreStore().menu_toggle), + }; let triggered = true; for (let prop in hotkey) if ( diff --git a/mods/core/render.js b/mods/core/render.js index fb1ec76..8b44c4f 100644 --- a/mods/core/render.js +++ b/mods/core/render.js @@ -168,46 +168,65 @@ module.exports = (store, __exports) => { }); document.addEventListener('keyup', (event) => { if (!electron.remote.getCurrentWindow().isFocused()) return; - // switch between tabs via key modifier - const select_tab_modifier = toKeyEvent( - store('e1692c29-475e-437b-b7ff-3eee872e1a42').select_modifier - ); - let triggered = true; - for (let prop in select_tab_modifier) - if (select_tab_modifier[prop] !== event[prop]) triggered = false; - if ( - triggered && - [ - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'ArrowRight', - 'ArrowLeft', - ].includes(event.key) - ) - this.selectTab(event.key); - // create/close tab keybindings - const new_tab_keybinding = toKeyEvent( - store('e1692c29-475e-437b-b7ff-3eee872e1a42').new_tab - ); - triggered = true; - for (let prop in new_tab_keybinding) - if (new_tab_keybinding[prop] !== event[prop]) triggered = false; - if (triggered) this.newTab(); - const close_tab_keybinding = toKeyEvent( - store('e1692c29-475e-437b-b7ff-3eee872e1a42').close_tab - ); - triggered = true; - for (let prop in close_tab_keybinding) - if (close_tab_keybinding[prop] !== event[prop]) triggered = false; - if (triggered && document.querySelector('.tab.current .close')) - document.querySelector('.tab.current .close').click(); + const tabStore = () => store('e1692c29-475e-437b-b7ff-3eee872e1a42'); + if (tabStore().select_modifier) { + // switch between tabs via key modifier + const select_tab_modifier = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().select_modifier), + }; + let triggered = true; + for (let prop in select_tab_modifier) + if (select_tab_modifier[prop] !== event[prop]) triggered = false; + if ( + triggered && + [ + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'ArrowRight', + 'ArrowLeft', + ].includes(event.key) + ) + this.selectTab(event.key); + } + if (tabStore().new_tab) { + // create/close tab keybindings + const new_tab_keybinding = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().new_tab), + }; + let triggered = true; + for (let prop in new_tab_keybinding) + if (new_tab_keybinding[prop] !== event[prop]) triggered = false; + if (triggered) this.newTab(); + } + if (tabStore().close_tab) { + const close_tab_keybinding = { + ctrlKey: false, + metaKey: false, + altKey: false, + shiftKey: false, + ...toKeyEvent(tabStore().close_tab), + }; + let triggered = true; + for (let prop in close_tab_keybinding) + if (close_tab_keybinding[prop] !== event[prop]) triggered = false; + if (triggered && document.querySelector('.tab.current .close')) + document.querySelector('.tab.current .close').click(); + } }); electron.ipcRenderer.on('enhancer:close-tab', (event, tab) => { this.closeTab(tab);