diff --git a/frontend/src/ts/input/handlers/keydown.ts b/frontend/src/ts/input/handlers/keydown.ts index 09f82c8c36b9..517d6d8e2524 100644 --- a/frontend/src/ts/input/handlers/keydown.ts +++ b/frontend/src/ts/input/handlers/keydown.ts @@ -1,6 +1,6 @@ import { Config } from "../../config/store"; import * as TestLogic from "../../test/test-logic"; -import { getCharFromEvent } from "../../test/layout-emulator"; +import { getCharFromEvent, mayEmulateChar } from "../../test/layout-emulator"; import { emulateInsertText } from "./insert-text"; import { showNoticeNotification, @@ -153,13 +153,13 @@ export async function onKeydown(event: KeyboardEvent): Promise { return; } - if (Config.layout !== "default") { + if (Config.layout !== "default" && mayEmulateChar(event)) { + event.preventDefault(); const emulatedChar = await getCharFromEvent(event); if (emulatedChar !== null) { await emulateInsertText({ data: emulatedChar, now }); - event.preventDefault(); - return; } + return; } if (event.key === "Tab") { diff --git a/frontend/src/ts/test/layout-emulator.ts b/frontend/src/ts/test/layout-emulator.ts index 4ed03c45324d..ebac42b5cad1 100644 --- a/frontend/src/ts/test/layout-emulator.ts +++ b/frontend/src/ts/test/layout-emulator.ts @@ -6,6 +6,166 @@ import { __nonReactive } from "../states/test"; let isAltGrPressed = false; const isPunctuationPattern = /\p{P}/u; +const ANSI_KEYCODES = [ + "Backquote", + "Digit1", + "Digit2", + "Digit3", + "Digit4", + "Digit5", + "Digit6", + "Digit7", + "Digit8", + "Digit9", + "Digit0", + "Minus", + "Equal", + "KeyQ", + "KeyW", + "KeyE", + "KeyR", + "KeyT", + "KeyY", + "KeyU", + "KeyI", + "KeyO", + "KeyP", + "BracketLeft", + "BracketRight", + "Backslash", + "KeyA", + "KeyS", + "KeyD", + "KeyF", + "KeyG", + "KeyH", + "KeyJ", + "KeyK", + "KeyL", + "Semicolon", + "Quote", + "KeyZ", + "KeyX", + "KeyC", + "KeyV", + "KeyB", + "KeyN", + "KeyM", + "Comma", + "Period", + "Slash", + "Space", +]; + +const ISO_KEYCODES = [ + "Backquote", + "Digit1", + "Digit2", + "Digit3", + "Digit4", + "Digit5", + "Digit6", + "Digit7", + "Digit8", + "Digit9", + "Digit0", + "Minus", + "Equal", + "KeyQ", + "KeyW", + "KeyE", + "KeyR", + "KeyT", + "KeyY", + "KeyU", + "KeyI", + "KeyO", + "KeyP", + "BracketLeft", + "BracketRight", + "KeyA", + "KeyS", + "KeyD", + "KeyF", + "KeyG", + "KeyH", + "KeyJ", + "KeyK", + "KeyL", + "Semicolon", + "Quote", + "Backslash", + "IntlBackslash", + "KeyZ", + "KeyX", + "KeyC", + "KeyV", + "KeyB", + "KeyN", + "KeyM", + "Comma", + "Period", + "Slash", + "Space", +]; + +const MATRIX_KEYCODES = [ + "Backquote", + "Digit1", + "Digit2", + "Digit3", + "Digit4", + "Digit5", + "Digit6", + "Digit7", + "Digit8", + "Digit9", + "Digit0", + "Minus", + "Equal", + "KeyQ", + "KeyW", + "KeyE", + "KeyR", + "KeyT", + "KeyY", + "KeyU", + "KeyI", + "KeyO", + "KeyP", + "BracketLeft", + "BracketRight", + "Backslash", + "KeyA", + "KeyS", + "KeyD", + "KeyF", + "KeyG", + "KeyH", + "KeyJ", + "KeyK", + "KeyL", + "Semicolon", + "Quote", + "KeyZ", + "KeyX", + "KeyC", + "KeyV", + "KeyB", + "KeyN", + "KeyM", + "Comma", + "Period", + "Slash", + "Space", +]; + +const EMULATED_KEY_CODES = new Set([ + ...ANSI_KEYCODES, + ...ISO_KEYCODES, + ...MATRIX_KEYCODES, +]); + export async function getCharFromEvent( event: KeyboardEvent, ): Promise { @@ -40,159 +200,11 @@ export async function getCharFromEvent( let keyEventCodes: string[] = []; if (layout.type === "ansi") { - keyEventCodes = [ - "Backquote", - "Digit1", - "Digit2", - "Digit3", - "Digit4", - "Digit5", - "Digit6", - "Digit7", - "Digit8", - "Digit9", - "Digit0", - "Minus", - "Equal", - "KeyQ", - "KeyW", - "KeyE", - "KeyR", - "KeyT", - "KeyY", - "KeyU", - "KeyI", - "KeyO", - "KeyP", - "BracketLeft", - "BracketRight", - "Backslash", - "KeyA", - "KeyS", - "KeyD", - "KeyF", - "KeyG", - "KeyH", - "KeyJ", - "KeyK", - "KeyL", - "Semicolon", - "Quote", - "KeyZ", - "KeyX", - "KeyC", - "KeyV", - "KeyB", - "KeyN", - "KeyM", - "Comma", - "Period", - "Slash", - "Space", - ]; + keyEventCodes = ANSI_KEYCODES; } else if (layout.type === "iso") { - keyEventCodes = [ - "Backquote", - "Digit1", - "Digit2", - "Digit3", - "Digit4", - "Digit5", - "Digit6", - "Digit7", - "Digit8", - "Digit9", - "Digit0", - "Minus", - "Equal", - "KeyQ", - "KeyW", - "KeyE", - "KeyR", - "KeyT", - "KeyY", - "KeyU", - "KeyI", - "KeyO", - "KeyP", - "BracketLeft", - "BracketRight", - "KeyA", - "KeyS", - "KeyD", - "KeyF", - "KeyG", - "KeyH", - "KeyJ", - "KeyK", - "KeyL", - "Semicolon", - "Quote", - "Backslash", - "IntlBackslash", - "KeyZ", - "KeyX", - "KeyC", - "KeyV", - "KeyB", - "KeyN", - "KeyM", - "Comma", - "Period", - "Slash", - "Space", - ]; + keyEventCodes = ISO_KEYCODES; } else if (layout.type === "matrix") { - keyEventCodes = [ - "Backquote", - "Digit1", - "Digit2", - "Digit3", - "Digit4", - "Digit5", - "Digit6", - "Digit7", - "Digit8", - "Digit9", - "Digit0", - "Minus", - "Equal", - "KeyQ", - "KeyW", - "KeyE", - "KeyR", - "KeyT", - "KeyY", - "KeyU", - "KeyI", - "KeyO", - "KeyP", - "BracketLeft", - "BracketRight", - "Backslash", - "KeyA", - "KeyS", - "KeyD", - "KeyF", - "KeyG", - "KeyH", - "KeyJ", - "KeyK", - "KeyL", - "Semicolon", - "Quote", - "KeyZ", - "KeyX", - "KeyC", - "KeyV", - "KeyB", - "KeyN", - "KeyM", - "Comma", - "Period", - "Slash", - "Space", - ]; + keyEventCodes = MATRIX_KEYCODES; } if (!keyEventCodes.includes(event.code)) { @@ -226,6 +238,10 @@ export async function getCharFromEvent( } } +export function mayEmulateChar(event: KeyboardEvent): boolean { + return EMULATED_KEY_CODES.has(event.code) || event.code.startsWith("Numpad"); +} + export function updateAltGrState(event: KeyboardEvent): void { const shouldHandleLeftAlt = event.code === "AltLeft" && navigator.userAgent.includes("Mac");