A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(input): cancel held web input on focus loss
yhhb45baae7319d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 62518afe1..4dfc48865 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Web focus loss cancels held pointer input and pending edges.** Blurring or hiding the game and replacing its input target releases host key, mouse, and touch tracking, clears stale render/fixed-step edges, and cancels UI presses without clicking them. The next touch can become the primary pointer, and late releases from the cancelled gesture are ignored.+- **Mini-game audio keeps playback ownership through device suspension.** Device-paused voices remain live for AudioSource completion, bus gain updates, and priority budgets. New play requests and manual resumes wait for device recovery; manual pauses withdraw automatic recovery, while stopped or despawned voices never restart.- **Async Box2D systems retain their physics performance category.** Physics stepping, interpolation, contact event delivery, and character controllers now appear under physics after module loading completes. Deferred system registration can explicitly provide its profiling domain without changing the classification of later registrations.diff --git a/sdk/src/input/input.ts b/sdk/src/input/input.tsindex e1979d0ff..755ad5f74 100644--- a/sdk/src/input/input.ts+++ b/sdk/src/input/input.ts@@ -461,6 +461,17 @@ export function inputEventCallbacks(state: InputState): InputEventCallbacks {onTouchCancel(id) {finishTouch(id, inputRouter.dispatchTouchCancel(id));},+ onInputCancel() {+ state.keysDown.clear();+ state.mouseButtons.clear();+ state.touches.clear();+ state.clearFrameState();+ state.keysPressedFixed.clear();+ state.keysReleasedFixed.clear();+ state.mouseButtonsPressedFixed.clear();+ state.mouseButtonsReleasedFixed.clear();+ cancelPrimaryPointer(state);+ },};}diff --git a/sdk/src/platform/types.ts b/sdk/src/platform/types.tsindex 6f4853df1..e040ca0db 100644--- a/sdk/src/platform/types.ts+++ b/sdk/src/platform/types.ts@@ -59,6 +59,8 @@ export interface InputEventCallbacks {onTouchMove?(id: number, x: number, y: number): void;onTouchEnd?(id: number): void;onTouchCancel?(id: number): void;+ /** Cancel held input and pending edges when the host loses focus or unbinds. */+ onInputCancel?(): void;}/**diff --git a/sdk/src/platform/web.ts b/sdk/src/platform/web.tsindex 67eff0e41..88bbbad7d 100644--- a/sdk/src/platform/web.ts+++ b/sdk/src/platform/web.ts@@ -182,6 +182,8 @@ class WebPlatformAdapter implements PlatformAdapter {const el = (target as HTMLElement) ?? document.querySelector('canvas') ?? document.body;const heldKeys = new Set<string>();+ const heldButtons = new Set<number>();+ const heldTouches = new Set<number>();const onKeyDown = (e: Event) => {const key = e as KeyboardEvent;const target = key.target instanceof HTMLElement ? key.target : null;@@ -200,17 +202,25 @@ class WebPlatformAdapter implements PlatformAdapter {const onBlur = () => {for (const code of heldKeys) callbacks.onKeyUp(code);heldKeys.clear();+ for (const button of heldButtons) callbacks.onPointerUp(button);+ heldButtons.clear();+ for (const id of heldTouches) pointer.cancel(id);+ heldTouches.clear();+ callbacks.onInputCancel?.();};+ const onVisibilityChange = () => { if (document.hidden) onBlur(); };const onMouseMove = (e: Event) => {const me = e as MouseEvent;callbacks.onPointerMove(me.offsetX, me.offsetY);};const onMouseDown = (e: Event) => {const me = e as MouseEvent;+ heldButtons.add(me.button);callbacks.onPointerDown(me.button, me.offsetX, me.offsetY);};const onMouseUp = (e: Event) => {- callbacks.onPointerUp((e as MouseEvent).button);+ const button = (e as MouseEvent).button;+ if (heldButtons.delete(button)) callbacks.onPointerUp(button);};const pointer = createPrimaryPointer(callbacks);@@ -220,6 +230,7 @@ class WebPlatformAdapter implements PlatformAdapter {const rect = (el as HTMLElement).getBoundingClientRect();for (let i = 0; i < te.changedTouches.length; i++) {const touch = te.changedTouches[i];+ heldTouches.add(touch.identifier);pointer.start(touch.identifier, touch.clientX - rect.left, touch.clientY - rect.top);}};@@ -236,13 +247,15 @@ class WebPlatformAdapter implements PlatformAdapter {e.preventDefault();const te = e as TouchEvent;for (let i = 0; i < te.changedTouches.length; i++) {- pointer.end(te.changedTouches[i].identifier);+ const id = te.changedTouches[i].identifier;+ if (heldTouches.delete(id)) pointer.end(id);}};const onTouchCancel = (e: Event) => {const te = e as TouchEvent;for (let i = 0; i < te.changedTouches.length; i++) {- pointer.cancel(te.changedTouches[i].identifier);+ const id = te.changedTouches[i].identifier;+ if (heldTouches.delete(id)) pointer.cancel(id);}};const onWheel = (e: Event) => {@@ -260,6 +273,7 @@ class WebPlatformAdapter implements PlatformAdapter {};window.addEventListener('blur', onBlur);+ document.addEventListener('visibilitychange', onVisibilityChange);document.addEventListener('keydown', onKeyDown);document.addEventListener('keyup', onKeyUp);el.addEventListener('mousemove', onMouseMove);@@ -273,6 +287,7 @@ class WebPlatformAdapter implements PlatformAdapter {this.inputCleanup_ = () => {window.removeEventListener('blur', onBlur);+ document.removeEventListener('visibilitychange', onVisibilityChange);onBlur();document.removeEventListener('keydown', onKeyDown);document.removeEventListener('keyup', onKeyUp);diff --git a/sdk/tests/web-focus-cancellation.test.ts b/sdk/tests/web-focus-cancellation.test.tsnew file mode 100644index 000000000..b5d9c9b76--- /dev/null+++ b/sdk/tests/web-focus-cancellation.test.ts@@ -0,0 +1,54 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { afterEach, expect, it, vi } from 'vitest';+import { webAdapter } from '../src/platform/web';+import { InputState, inputEventCallbacks } from '../src/input/input';+import { UiPointerBook, uiPointersOf } from '../src/ui/input/pointerBook';+import type { Entity } from '../src/types';++afterEach(() => { webAdapter.unbindInputEvents(); document.body.replaceChildren(); vi.restoreAllMocks(); });+function bind() {+ const canvas = document.createElement('canvas'); document.body.appendChild(canvas);+ const input = new InputState(); webAdapter.bindInputEvents(inputEventCallbacks(input), canvas);+ return { canvas, input };+}+function touch(canvas: HTMLCanvasElement, type: string, ids: number[]) {+ const event = new Event(type, { cancelable: true });+ Object.defineProperty(event, 'changedTouches', { value: ids.map(identifier => ({ identifier, clientX: 5, clientY: 5 })) });+ canvas.dispatchEvent(event);+}+it('blur cancels a held mouse without a UI click or stale fixed-step edges', () => {+ const { canvas, input } = bind(), book = new UiPointerBook(), entity = 1 as Entity;+ canvas.dispatchEvent(new MouseEvent('mousedown', { button: 0 }));+ book.step(uiPointersOf(input), () => entity, () => true);+ window.dispatchEvent(new Event('blur'));+ expect(input.isMouseButtonDown(0)).toBe(false);+ const events = book.step(uiPointersOf(input), () => entity, () => true);+ expect(events.some(e => e.kind === 'click')).toBe(false); expect(book.pressed.size).toBe(0);+ input.beginFixedStep(); expect(input.isMouseButtonPressed(0)).toBe(false); input.endFixedStep();+ document.dispatchEvent(new MouseEvent('mouseup', { button: 0 }));+ expect(input.isMouseButtonReleased(0)).toBe(false);+});+it('blur cancels every touch and allows the next finger to become primary', () => {+ const { canvas, input } = bind(), book = new UiPointerBook();+ touch(canvas, 'touchstart', [1, 2]); book.step(uiPointersOf(input), () => 1 as Entity, () => true);+ window.dispatchEvent(new Event('blur'));+ expect(input.touches.size).toBe(0); expect(input.touchesEnded.size).toBe(0);+ expect(book.step(uiPointersOf(input), () => 1 as Entity, () => true).some(e => e.kind === 'click')).toBe(false);+ touch(canvas, 'touchstart', [3]); expect(input.isMouseButtonDown(0)).toBe(true);+});+it('a hidden page cancels held keys and buttons before another game frame', () => {+ const { canvas, input } = bind();+ document.dispatchEvent(new KeyboardEvent('keydown', { code: 'KeyD' }));+ canvas.dispatchEvent(new MouseEvent('mousedown', { button: 2 }));+ vi.spyOn(document, 'hidden', 'get').mockReturnValue(true);+ document.dispatchEvent(new Event('visibilitychange'));+ expect(input.isKeyDown('KeyD')).toBe(false); expect(input.isKeyPressed('KeyD')).toBe(false);+ expect(input.isMouseButtonDown(2)).toBe(false);+});+it('unbind cancels the old input before a new target receives its own presses', () => {+ const first = bind(); first.canvas.dispatchEvent(new MouseEvent('mousedown', { button: 0 }));+ const second = bind(); expect(first.input.isMouseButtonDown(0)).toBe(false);+ second.canvas.dispatchEvent(new MouseEvent('mousedown', { button: 0 }));+ expect(second.input.isMouseButtonDown(0)).toBe(true);+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 750dc6d4c..01b6bc597 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(input): cancel held web input on focus loss':+ { note: 'Web focus loss cancels held pointer input and pending edges.' },'fix(audio): retain mini-game playback intent during device suspension':{ note: 'Mini-game audio keeps playback ownership through device suspension.' },'fix(profiling): attribute async Box2D systems to physics':