Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/src/ts/input/handlers/keydown.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -153,13 +153,13 @@ export async function onKeydown(event: KeyboardEvent): Promise<void> {
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") {
Expand Down
318 changes: 167 additions & 151 deletions frontend/src/ts/test/layout-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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");
Expand Down
Loading