From c78e2cd21420537d5110c41d7aa333eb55f6c808 Mon Sep 17 00:00:00 2001 From: Joe-Dowd Date: Fri, 20 Mar 2020 19:39:37 +0000 Subject: [PATCH] Fixed mod keys when setting keybinding --- src/options.ts | 77 ++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/options.ts b/src/options.ts index 6586eddf..88213ea2 100644 --- a/src/options.ts +++ b/src/options.ts @@ -307,7 +307,7 @@ function activateKeybindChange(element: HTMLElement) { element.querySelector(".option-hidden-section").classList.remove("hidden"); - document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); + document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); } /** @@ -318,46 +318,49 @@ function activateKeybindChange(element: HTMLElement) { */ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { var key = e.key; + if (["Shift", "Control", "Meta", "Alt"].indexOf(key) !== -1) { + document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); + } else { + let button = element.querySelector(".trigger-button"); + let option = element.getAttribute("sync-option"); - let button = element.querySelector(".trigger-button"); - let option = element.getAttribute("sync-option"); + // Don't allow keys which are already listened for by youtube + let restrictedKeys = "1234567890,.jklftcbmJKLFTCBM/<> -"; + if (restrictedKeys.indexOf(key) !== -1 ) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key " + key + " is already used by youtube. Please select another key."); + return; + } + + // Make sure keybind isn't used by the other listener + // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. + let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; + if (key === otherKeybind) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key " + key + " is bound to another action. Please select another key."); + return; + } + + // cancel setting a keybind + if (key === "Escape") { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + return; + } + + + Config.config[option] = key; + + let status = element.querySelector(".option-hidden-section > .keybind-status"); + status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete"); + + let statusKey = element.querySelector(".option-hidden-section > .keybind-status-key"); + statusKey.innerText = key; - // Don't allow keys which are already listened for by youtube - let restrictedKeys = Array.from("1234567890,.jklfcbm/<> -").concat(["Shift"]); - if (restrictedKeys.indexOf(key) !== -1 ) { - element.querySelector(".option-hidden-section").classList.add("hidden"); button.classList.remove("disabled"); - alert("The key '" + key + "' is already used by youtube. Please select another key."); - return; } - - // Make sure keybind isn't used by the other listener - // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. - let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; - if (key === otherKeybind) { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); - alert("The key '" + key + "' is bound to another action. Please select another key."); - return; - } - - // cancel setting a keybind - if (key === "Escape") { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); - return; - } - - - Config.config[option] = key; - - let status = element.querySelector(".option-hidden-section > .keybind-status"); - status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete"); - - let statusKey = element.querySelector(".option-hidden-section > .keybind-status-key"); - statusKey.innerText = key; - - button.classList.remove("disabled"); } /**