From b96cee1012e13d602791bbf30343c684cecca580 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 7 Dec 2017 02:11:50 +0300 Subject: [PATCH] use Event#key for hotkey recognition in manager and popup should help #286 --- manage/incremental-search.js | 4 ++-- popup/hotkeys.js | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/manage/incremental-search.js b/manage/incremental-search.js index 9dc5312c..1d6f75a0 100644 --- a/manage/incremental-search.js +++ b/manage/incremental-search.js @@ -75,9 +75,9 @@ onDOMready().then(() => { event.target.matches('[type="text"], [type="search"]')) { return; } - const k = event.which; + const {which: k, key} = event; // focus search field on "/" key - if (k === 191 && !event.shiftKey) { + if (key === '/' || !key && k === 191 && !event.shiftKey) { event.preventDefault(); $('#search').focus(); return; diff --git a/popup/hotkeys.js b/popup/hotkeys.js index dedf491b..da7364cb 100644 --- a/popup/hotkeys.js +++ b/popup/hotkeys.js @@ -34,23 +34,24 @@ var hotkeys = (() => { return; } let entry; - const k = event.which; - if (k >= 48 && k <= 57 || k >= 96 && k <= 105) { + const {which: k, key} = event; + if (key ? key >= '0' && key <= '9' : k >= 48 && k <= 57 || k >= 96 && k <= 105) { // 0-9, numpad 0-9 - entry = installed.children[k === 48 || k === 96 ? 9 : k - (k > 96 ? 97 : 49)]; - } else if (k >= 65 && k <= 90) { - // a-z - const letter = new RegExp('^\\x' + k.toString(16), 'i'); - entry = [...installed.children].find(entry => letter.test(entry.textContent)); - } else if (k === 192 || k === 106) { + const i = key === '0' ? 9 : key ? Number(key) - 1 : k === 48 || k === 96 ? 9 : k - (k > 96 ? 97 : 49); + entry = installed.children[i]; + } else if (key ? key === '`' || key === '*' && !event.shiftKey : k === 192 || k === 106) { // backtick ` and numpad * invertTogglables(); - } else if (k === 109) { + } else if (key ? key === '-' : k === 109) { // numpad - toggleState(installed.children, 'enabled', false); - } else if (k === 107) { + } else if (key ? key === '+' : k === 107) { // numpad + toggleState(installed.children, 'disabled', true); + } else if (key ? key.length === 1 : k >= 65 && k <= 90) { + // any single character + const letter = new RegExp(key ? '^' + key : '^\\x' + k.toString(16), 'i'); + entry = [...installed.children].find(entry => letter.test(entry.textContent)); } if (!entry) { return;