From 78a3644c4584e3c2860a9dd11e6a036f0ff27129 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Sun, 14 Aug 2022 13:57:01 -0700 Subject: [PATCH] Fix logic bug in key internationalization for A-Z keys in alternate layouts --- .../src/utility-functions/keyboard-entry.ts | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/frontend/src/utility-functions/keyboard-entry.ts b/frontend/src/utility-functions/keyboard-entry.ts index 70cf92b1..a47c763b 100644 --- a/frontend/src/utility-functions/keyboard-entry.ts +++ b/frontend/src/utility-functions/keyboard-entry.ts @@ -71,8 +71,7 @@ export async function getLocalizedScanCode(e: KeyboardEvent): Promise { // Use good-enough-for-now heuristics on the writing system keys, which are commonly subject to change by locale // Number scan codes - const scanCodeDigit = /^Digit[0-9]$/.test(scanCode); - if (scanCodeDigit) { + if (/^Digit[0-9]$/.test(scanCode)) { // For now it's good enough to treat every digit key, regardless of locale, as just its digit from the standard US layout. // Even on a keyboard like the French AZERTY layout, where numbers are shifted, users still refer to those keys by their numbers. // This unfortunately means that any special symbols under these keys are overridden by their number, making it impossible to access some shortcuts that rely on those special symbols. @@ -81,20 +80,15 @@ export async function getLocalizedScanCode(e: KeyboardEvent): Promise { } // Letter scan codes - const scanCodeLetter = scanCode.match(/^Key([A-Z])$/); - if (scanCodeLetter) { - const scanCodeLetterValue = String(scanCodeLetter[1]); + if (/^Key([A-Z])$/.test(scanCode)) { + // Get the uppercase letter, with any accents or discritics removed if possible + const rawLetter = keyText + .normalize("NFD") + .replace(/\p{Diacritic}/gu, "") + .toUpperCase(); - // If the scan code matches the key letter (ignoring diacritics and case), use that letter directly - const letterOfScanCodeMatchesKey = - scanCodeLetterValue === - keyText - .normalize("NFD") - .replace(/\p{Diacritic}/gu, "") - .toUpperCase(); - if (letterOfScanCodeMatchesKey) { - return scanCode; - } + // If the key letter is in the A-Z range, use the key code for that letter + if (/^[A-Z]$/.test(rawLetter)) return `Key${rawLetter}`; // If the key text isn't one of the named attribute values, that means it must be the literal unicode value which we use directly // It is likely a weird symbol that isn't in the A-Z range even with accents removed.