From c364714c8188ab1503b53b096a70b1fd5b6b3055 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 26 Sep 2021 18:14:00 -0400 Subject: Fix hotkey input field conflict (#1963) * Move comment * Add DocumentUtil.isInputElement * Use DocumentUtil.isInputElement * Fix simple hotkeys (shift or no modifier) preventing text field input * Improve input detection * Validate key is an input character before blocking hotkey * Simplify * Fix incorrect property --- ext/js/input/hotkey-handler.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'ext/js/input') diff --git a/ext/js/input/hotkey-handler.js b/ext/js/input/hotkey-handler.js index 8388302d..6c63aebb 100644 --- a/ext/js/input/hotkey-handler.js +++ b/ext/js/input/hotkey-handler.js @@ -153,11 +153,10 @@ class HotkeyHandler extends EventDispatcher { // Private _onKeyDown(e) { - const key = e.code; - const hotkeyInfo = this._hotkeys.get(key); + const hotkeyInfo = this._hotkeys.get(e.code); if (typeof hotkeyInfo !== 'undefined') { const eventModifiers = DocumentUtil.getActiveModifiers(e); - if (this._invokeHandlers(eventModifiers, hotkeyInfo)) { + if (this._invokeHandlers(eventModifiers, hotkeyInfo, e.key)) { e.preventDefault(); return; } @@ -165,9 +164,9 @@ class HotkeyHandler extends EventDispatcher { this.trigger('keydownNonHotkey', e); } - _invokeHandlers(modifiers, hotkeyInfo) { + _invokeHandlers(modifiers, hotkeyInfo, key) { for (const {modifiers: handlerModifiers, action, argument} of hotkeyInfo.handlers) { - if (!this._areSame(handlerModifiers, modifiers)) { continue; } + if (!this._areSame(handlerModifiers, modifiers) || !this._isHotkeyPermitted(modifiers, key)) { continue; } const actionHandler = this._actions.get(action); if (typeof actionHandler !== 'undefined') { @@ -223,4 +222,16 @@ class HotkeyHandler extends EventDispatcher { this._eventListeners.removeAllEventListeners(); } } + + _isHotkeyPermitted(modifiers, key) { + return !( + (modifiers.length === 0 || (modifiers.length === 1 && modifiers[0] === 'shift')) && + DocumentUtil.isInputElementFocused() && + this._isKeyCharacterInput(key) + ); + } + + _isKeyCharacterInput(key) { + return key.length === 1; + } } -- cgit v1.2.3