Default keyboard labels to QWERTY

This commit is contained in:
Victor Bonnet
2026-08-29 03:41:12 +00:00
committed by tfrere
parent 5d813593e1
commit 9ce679a61d
2 changed files with 31 additions and 24 deletions
+14 -11
View File
@@ -1,9 +1,7 @@
// Controls use physical KeyboardEvent.code positions; getLayoutMap() provides
// the characters printed on those keys for the active layout.
const FALLBACK_LABELS = Object.freeze({ KeyW: "W/Z", KeyA: "A/Q", KeyQ: "Q/A" });
const MOVEMENT_CODES = ["KeyW", "KeyA", "KeyS", "KeyD"];
const MOVEMENT_FALLBACK_HINT = "arrows or WASD / ZQSD";
const MOVEMENT_FALLBACK_HINT = "arrows or WASD";
function keycap(value) {
if (typeof value !== "string") return null;
@@ -14,22 +12,27 @@ function keycap(value) {
}
export function resolveKeycaps(codes, layoutMap) {
const required = [...new Set([...MOVEMENT_CODES, ...codes])];
const labels = {};
const fromLayout = new Set();
for (const code of new Set([...MOVEMENT_CODES, ...codes])) {
let complete = true;
for (const code of required) {
let label = null;
try {
label = keycap(layoutMap?.get?.(code));
} catch {
label = null;
}
if (label) fromLayout.add(code);
labels[code] = label ?? FALLBACK_LABELS[code] ?? code.replace(/^Key/, "");
if (!label) complete = false;
labels[code] = label;
}
const moveHint = MOVEMENT_CODES.every((code) => fromLayout.has(code))
? `arrows or ${MOVEMENT_CODES.map((code) => labels[code]).join("")}`
: MOVEMENT_FALLBACK_HINT;
return { labels, moveHint };
if (!complete) {
for (const code of required) labels[code] = code.replace(/^Key/, "");
return { labels, moveHint: MOVEMENT_FALLBACK_HINT };
}
return {
labels,
moveHint: `arrows or ${MOVEMENT_CODES.map((code) => labels[code]).join("")}`,
};
}
export async function readLayoutMap(nav = globalThis.navigator) {
+17 -13
View File
@@ -31,35 +31,39 @@ test("resolves caps and the movement hint from the active layout", () => {
assert.equal(dvorak.moveHint, "arrows or ,AOE");
});
test("falls back to labels naming both layouts without a map", () => {
test("falls back to QWERTY labels without a map", () => {
assert.deepEqual(resolveKeycaps(CODES, null), {
labels: { KeyW: "W/Z", KeyA: "A/Q", KeyS: "S", KeyD: "D", KeyQ: "Q/A", KeyE: "E" },
moveHint: "arrows or WASD / ZQSD",
labels: { KeyW: "W", KeyA: "A", KeyS: "S", KeyD: "D", KeyQ: "Q", KeyE: "E" },
moveHint: "arrows or WASD",
});
assert.equal(resolveKeycaps(["KeyX"], null).labels.KeyX, "X");
});
test("keeps the inclusive hint when the map misses a movement key", () => {
const ALL_QWERTY = {
labels: { KeyW: "W", KeyA: "A", KeyS: "S", KeyD: "D", KeyQ: "Q", KeyE: "E" },
moveHint: "arrows or WASD",
};
test("falls back entirely to QWERTY when the map misses a movement key", () => {
const partial = new Map([["KeyW", "z"], ["KeyA", "q"], ["KeyS", "s"]]);
const { labels, moveHint } = resolveKeycaps(CODES, partial);
assert.equal(labels.KeyW, "Z");
assert.equal(labels.KeyD, "D");
assert.equal(moveHint, "arrows or WASD / ZQSD");
assert.deepEqual(resolveKeycaps(CODES, partial), ALL_QWERTY);
});
test("falls back entirely to QWERTY when the map misses a requested key", () => {
const partial = new Map([...AZERTY].filter(([code]) => code !== "KeyE"));
assert.deepEqual(resolveKeycaps(CODES, partial), ALL_QWERTY);
});
test("rejects layout values that cannot sit on a keycap", () => {
const junk = new Map(
Object.entries({ KeyQ: "", KeyE: " ", KeyW: "Dead", KeyA: "́", KeyS: 7 }),
);
const { labels } = resolveKeycaps(CODES, junk);
assert.deepEqual(labels, {
KeyW: "W/Z", KeyA: "A/Q", KeyS: "S", KeyD: "D", KeyQ: "Q/A", KeyE: "E",
});
assert.deepEqual(resolveKeycaps(CODES, junk), ALL_QWERTY);
});
test("a throwing map cannot break the menu", () => {
const hostile = { get: () => { throw new Error("nope"); } };
assert.equal(resolveKeycaps(CODES, hostile).labels.KeyQ, "Q/A");
assert.deepEqual(resolveKeycaps(CODES, hostile), ALL_QWERTY);
});
test("readLayoutMap yields null when the API is absent or refuses", async () => {