From ff3c5fd137de2c16bba78b50c932d4713f28cf7f Mon Sep 17 00:00:00 2001 From: typer Date: Wed, 9 Sep 2020 04:36:43 -0700 Subject: [PATCH] Fixes a bug where caps lock was not overridden in caps lock backspace mode. --- public/js/script.js | 43 +++++++++++++++++++++++++++-------------- public/js/userconfig.js | 2 +- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/public/js/script.js b/public/js/script.js index 1670b9d1e..67307e683 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -563,14 +563,34 @@ function setToggleSettings(state) { } function emulateLayout(event) { - function shouldShiftKey(event, newKeyPreview) { + function emulatedLayoutShouldShiftKey(event, newKeyPreview) { if (config.capsLockBackspace) return event.shiftKey; const isCapsLockHeld = event.originalEvent.getModifierState("CapsLock"); if (isCapsLockHeld) return isASCIILetter(newKeyPreview) !== event.shiftKey; return event.shiftKey; } - if (config.layout === "default" || event.key === " " || event.key === "Enter") + function replaceEventKey(event, keyCode) { + const newKey = String.fromCharCode(keyCode); + event.keyCode = keyCode; + event.charCode = keyCode; + event.which = keyCode; + event.key = newKey; + event.code = "Key" + newKey.toUpperCase(); + } + if (event.key === " " || event.key === "Enter") return event; + if (config.layout === "default") { + //override the shift modifier for the default layout if needed + if (config.capsLockBackspace && isASCIILetter(event.key)) { + replaceEventKey( + event, + event.shiftKey + ? event.key.toUpperCase().charCodeAt(0) + : event.key.toLowerCase().charCodeAt(0) + ); + } + return event; + } const qwertyMasterLayout = { Backquote: "`~", Digit1: "1!", @@ -621,27 +641,22 @@ function emulateLayout(event) { Slash: "/?", Space: " ", }; - let layoutMap = layouts[config.layout]; - let qwertyMap = layouts["qwerty"]; + const layoutMap = layouts[config.layout]; + const qwertyMap = layouts["qwerty"]; - let qwertyKey = qwertyMasterLayout[event.code]; + const qwertyKey = qwertyMasterLayout[event.code]; let mapIndex; - let newKey; for (let i = 0; i < qwertyMap.length; i++) { const key = qwertyMap[i]; - let keyIndex = key.indexOf(qwertyKey); + const keyIndex = key.indexOf(qwertyKey); if (keyIndex != -1) { mapIndex = i; } } const newKeyPreview = layoutMap[mapIndex][0]; - const shift = shouldShiftKey(event, newKeyPreview) ? 1 : 0; - newKey = layoutMap[mapIndex][shift]; - event.keyCode = newKey.charCodeAt(0); - event.charCode = newKey.charCodeAt(0); - event.which = newKey.charCodeAt(0); - event.key = newKey; - event.code = "Key" + newKey.toUpperCase(); + const shift = emulatedLayoutShouldShiftKey(event, newKeyPreview) ? 1 : 0; + const newKey = layoutMap[mapIndex][shift]; + replaceEventKey(event, newKey.charCodeAt(0)); return event; } diff --git a/public/js/userconfig.js b/public/js/userconfig.js index 6de613c50..e072e0105 100644 --- a/public/js/userconfig.js +++ b/public/js/userconfig.js @@ -873,7 +873,7 @@ function changeLanguage(language, nosave) { } function setCapsLockBackspace(capsLockBackspace, nosave) { - if (capsLockBackspace == null || capsLockBackspace == undefined) { + if (capsLockBackspace === null || capsLockBackspace === undefined) { capsLockBackspace = false; } config.capsLockBackspace = capsLockBackspace;