Fix previous commit by mapping codes to keys

This commit is contained in:
Keavon Chambers 2021-12-23 16:02:54 -08:00
parent 05737202fa
commit 3500160bf7
1 changed files with 40 additions and 2 deletions

View File

@ -38,6 +38,7 @@ export function createInputManager(editor: EditorState, container: HTMLElement,
if (dialog.dialogIsVisible()) return false; if (dialog.dialogIsVisible()) return false;
const key = getLatinKey(e); const key = getLatinKey(e);
if (!key) return false;
// Don't redirect a fullscreen request // Don't redirect a fullscreen request
if (key === "f11" && e.type === "keydown" && !e.repeat) { if (key === "f11" && e.type === "keydown" && !e.repeat) {
@ -61,6 +62,7 @@ export function createInputManager(editor: EditorState, container: HTMLElement,
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
const key = getLatinKey(e); const key = getLatinKey(e);
if (!key) return;
if (shouldRedirectKeyboardEventToBackend(e)) { if (shouldRedirectKeyboardEventToBackend(e)) {
e.preventDefault(); e.preventDefault();
@ -82,6 +84,7 @@ export function createInputManager(editor: EditorState, container: HTMLElement,
const onKeyUp = (e: KeyboardEvent) => { const onKeyUp = (e: KeyboardEvent) => {
const key = getLatinKey(e); const key = getLatinKey(e);
if (!key) return;
if (shouldRedirectKeyboardEventToBackend(e)) { if (shouldRedirectKeyboardEventToBackend(e)) {
e.preventDefault(); e.preventDefault();
@ -204,7 +207,7 @@ export function makeModifiersBitfield(e: WheelEvent | PointerEvent | KeyboardEve
} }
// This function is a naive, temporary solution to allow non-Latin keyboards to fall back on the physical QWERTY layout // This function is a naive, temporary solution to allow non-Latin keyboards to fall back on the physical QWERTY layout
function getLatinKey(e: KeyboardEvent): string { function getLatinKey(e: KeyboardEvent): string | null {
const key = e.key.toLowerCase(); const key = e.key.toLowerCase();
const isPrintable = isKeyPrintable(e.key); const isPrintable = isKeyPrintable(e.key);
@ -213,12 +216,47 @@ function getLatinKey(e: KeyboardEvent): string {
// These non-Latin characters should fall back to the Latin equivalent at the key location // These non-Latin characters should fall back to the Latin equivalent at the key location
const LAST_LATIN_UNICODE_CHAR = 0x024f; const LAST_LATIN_UNICODE_CHAR = 0x024f;
if (key.length > 1 || key.charCodeAt(0) > LAST_LATIN_UNICODE_CHAR) return e.code.toLowerCase(); if (key.length > 1 || key.charCodeAt(0) > LAST_LATIN_UNICODE_CHAR) return keyCodeToKey(e.code);
// Otherwise, ths is a printable Latin character // Otherwise, ths is a printable Latin character
return e.key.toLowerCase(); return e.key.toLowerCase();
} }
function keyCodeToKey(code: string): string | null {
// Letters
if (code.match(/^Key[A-Z]$/)) return code.replace("Key", "").toLowerCase();
// Numbers
if (code.match(/^Digit[0-9]$/)) return code.replace("Digit", "");
if (code.match(/^Numpad[0-9]$/)) return code.replace("Numpad", "");
// Function keys
if (code.match(/^F[1-9]|F1[0-9]|F20$/)) return code.replace("F", "").toLowerCase();
// Other characters
const mapping: Record<string, string> = {
BracketLeft: "[",
BracketRight: "]",
Backslash: "\\",
Slash: "/",
Period: ".",
Comma: ",",
Equal: "=",
Minus: "-",
Quote: "'",
Semicolon: ";",
NumpadEqual: "=",
NumpadDivide: "/",
NumpadMultiply: "*",
NumpadSubtract: "-",
NumpadAdd: "+",
NumpadDecimal: ".",
};
if (code in mapping) return mapping[code];
return null;
}
function isKeyPrintable(key: string): boolean { function isKeyPrintable(key: string): boolean {
const allPrintableKeys: string[] = [ const allPrintableKeys: string[] = [
// Modifier // Modifier