From a0ab667db9441ae5c5a6516336de72504a0184db Mon Sep 17 00:00:00 2001 From: Francis Ashley Date: Sun, 25 Oct 2026 10:51:45 +0000 Subject: [PATCH] fix(input): clear modifiers whose keyup was missed --- src/client/InputHandler.ts | 36 +++++++++++++++++++++++++++++ tests/InputHandler.test.ts | 47 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/client/InputHandler.ts b/src/client/InputHandler.ts index f9c18cd677d..95908aa5cd2 100644 --- a/src/client/InputHandler.ts +++ b/src/client/InputHandler.ts @@ -426,6 +426,38 @@ export class InputHandler { this.multiSelectionActive = false; } + /** + * Ensures activeKeys only holds modifiers that the event reports as pressed, so a + * modifier whose keyup was missed can't keep acting as if it's held. + * See https://github.com/openfrontio/OpenFrontIO/issues/6122 + */ + private clearReleasedModifiers(e: MouseEvent | KeyboardEvent) { + const held: Record = { + MetaLeft: e.metaKey, + MetaRight: e.metaKey, + ShiftLeft: e.shiftKey, + ShiftRight: e.shiftKey, + AltLeft: e.altKey, + AltRight: e.altKey, + ControlLeft: e.ctrlKey, + ControlRight: e.ctrlKey, + }; + // Drop modifiers the event says aren't held. + for (const code of this.activeKeys) { + if (held[code] === false) this.activeKeys.delete(code); + } + // The crosshair was set by one of these; clear it once none still apply. + const crosshairNeeded = + this.activeKeys.has(this.keybinds.boxSelectWarships) || // box-select key held + this.selectionBoxActive || // box-select drag in progress + this.unitSelectionActive || // warship(s) selected + this.multiSelectionActive || // warships selected by box + this.longPressActive; // touch long-press selection + if (!crosshairNeeded) { + this.canvas.style.cursor = ""; + } + } + /** Re-read the player's keybinds and rebuild the key dispatch table. */ private buildKeybindTable() { this.keybinds = this.userSettings.keybinds(Platform.isMac); @@ -740,6 +772,7 @@ export class InputHandler { window.addEventListener( "keydown", (e) => { + this.clearReleasedModifiers(e); const isTextInput = this.isTextInputTarget(e.target); if (isTextInput && e.code !== "Escape") { return; @@ -951,6 +984,7 @@ export class InputHandler { } private onPointerDown(event: PointerEvent) { + this.clearReleasedModifiers(event); if (event.button === 1) { event.preventDefault(); this.eventBus.emit(new AutoUpgradeEvent(event.clientX, event.clientY)); @@ -1072,6 +1106,7 @@ export class InputHandler { }; onPointerUp(event: PointerEvent) { + this.clearReleasedModifiers(event); if (event.button === 1) { event.preventDefault(); return; @@ -1248,6 +1283,7 @@ export class InputHandler { } private onPointerMove(event: PointerEvent) { + this.clearReleasedModifiers(event); if (event.button === 1) { event.preventDefault(); return; diff --git a/tests/InputHandler.test.ts b/tests/InputHandler.test.ts index 4ee8a9ebedd..c90ee1a0754 100644 --- a/tests/InputHandler.test.ts +++ b/tests/InputHandler.test.ts @@ -41,6 +41,8 @@ class MockPointerEvent { pointerType: string; ctrlKey: boolean; shiftKey: boolean; + metaKey: boolean; + altKey: boolean; preventDefault: () => void; constructor(type: string, init: any) { @@ -54,6 +56,8 @@ class MockPointerEvent { this.pointerType = init.pointerType ?? "mouse"; this.ctrlKey = init.ctrlKey ?? false; this.shiftKey = init.shiftKey ?? false; + this.metaKey = init.metaKey ?? false; + this.altKey = init.altKey ?? false; this.preventDefault = vi.fn(); } } @@ -353,11 +357,12 @@ describe("InputHandler AutoUpgrade", () => { }); } - function fireLeftPointerUp(ctrlKey: boolean) { + function fireLeftPointerUp(ctrlKey: boolean, metaKey = false) { const shared = { button: 0 as const, pointerId: 1, ctrlKey, + metaKey, }; // Matching pointerdown required: onPointerUp returns early unless // pointerDown is set and pointers has this pointerId. @@ -464,7 +469,7 @@ describe("InputHandler AutoUpgrade", () => { inputHandler["activeKeys"].add("MetaLeft"); const mockEmit = vi.spyOn(eventBus, "emit"); - fireLeftPointerUp(false); + fireLeftPointerUp(false, true); const emittedTypes = mockEmit.mock.calls.map( (call) => call[0].constructor.name, @@ -473,6 +478,39 @@ describe("InputHandler AutoUpgrade", () => { expect(emittedTypes).not.toContain("MouseUpEvent"); }); + test("Mac: a Cmd whose keyup was swallowed (cmd+shift+4) does not open the build menu", () => { + setIsMac(true); + inputHandler.initialize(); + inputHandler["keybinds"].buildMenuModifier = "MetaLeft"; + // The screenshot overlay takes the keyboard after these keydowns, so + // their keyups never reach the page and the window never blurs. + window.dispatchEvent( + new KeyboardEvent("keydown", { code: "MetaLeft", metaKey: true }), + ); + window.dispatchEvent( + new KeyboardEvent("keydown", { + code: "ShiftLeft", + metaKey: true, + shiftKey: true, + }), + ); + expect(inputHandler["activeKeys"].has("MetaLeft")).toBe(true); + expect(mockCanvas.style.cursor).toBe("crosshair"); + + const mockEmit = vi.spyOn(eventBus, "emit"); + fireLeftPointerUp(false); + + const emittedTypes = mockEmit.mock.calls.map( + (call) => call[0].constructor.name, + ); + expect(emittedTypes).not.toContain("ShowBuildMenuEvent"); + expect(emittedTypes).toContain("MouseUpEvent"); + expect(inputHandler["activeKeys"].has("MetaLeft")).toBe(false); + expect(inputHandler["activeKeys"].has("ShiftLeft")).toBe(false); + expect(mockCanvas.style.cursor).toBe(""); + inputHandler.destroy(); + }); + test("Mac: ctrl+left during spawn still emits MouseUpEvent", () => { setIsMac(true); mockGameView.inSpawnPhase = () => true; @@ -1688,6 +1726,7 @@ describe("Warship box selection (Shift+drag)", () => { inputHandler["onPointerMove"]( new PointerEvent("pointermove", { button: 0, + shiftKey: true, clientX: 200, clientY: 200, pointerId: 1, @@ -1720,6 +1759,7 @@ describe("Warship box selection (Shift+drag)", () => { inputHandler["onPointerMove"]( new PointerEvent("pointermove", { button: 0, + shiftKey: true, clientX: 200, clientY: 200, pointerId: 1, @@ -1730,6 +1770,7 @@ describe("Warship box selection (Shift+drag)", () => { inputHandler["onPointerUp"]( new PointerEvent("pointerup", { button: 0, + shiftKey: true, clientX: 200, clientY: 200, pointerId: 1, @@ -1771,6 +1812,7 @@ describe("Warship box selection (Shift+drag)", () => { inputHandler["onPointerMove"]( new PointerEvent("pointermove", { button: 0, + shiftKey: true, clientX: 104, clientY: 104, pointerId: 1, @@ -1779,6 +1821,7 @@ describe("Warship box selection (Shift+drag)", () => { inputHandler["onPointerUp"]( new PointerEvent("pointerup", { button: 0, + shiftKey: true, clientX: 104, clientY: 104, pointerId: 1,