Skip to content

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.

fix(input): reset native input when the host hides

yhhf776d077017d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c0321cd1..b179aac25 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Native input resets when the host hides.** The native adapter cancels held touches, mouse buttons, keys and frame edges on hide or input teardown, resets the primary finger, removes its hide subscription and ignores late releases from the previous gesture.
+
- **Reused touch IDs preserve both gestures.** When a host reuses a touch ID between frames, UI releases the old gesture before capturing the new one, retains quick-tap clicks and projects the held finger's current position for dragging and scrolling.
- **Cancelled touch input no longer becomes a click.** Shared touch forwarding distinguishes primary cancellation from normal release, clears cancelled touch and fixed-step edges, and preserves other fingers. Mini-game hide and input teardown cancel held gestures and keys, reset the primary finger, and unregister the hide callback so resumed input starts cleanly.
diff --git a/sdk/src/platform/native/adapter.ts b/sdk/src/platform/native/adapter.ts
index dd22ec952..b170584c3 100644
--- a/sdk/src/platform/native/adapter.ts
+++ b/sdk/src/platform/native/adapter.ts
@@ -152,22 +152,39 @@ export class NativePlatformAdapter implements PlatformAdapter {
bindInputEvents(callbacks: InputEventCallbacks): void {
this.inputCleanup_?.();
const pointer = createPrimaryPointer(callbacks);
+ const heldKeys = new Set<string>();
+ const heldTouches = new Set<number>();
+ const heldButtons = new Set<number>();
+ const cancelInput = (): void => {
+ for (const code of heldKeys) callbacks.onKeyUp(code);
+ heldKeys.clear();
+ for (const id of heldTouches) pointer.cancel(id);
+ heldTouches.clear();
+ heldButtons.clear();
+ callbacks.onInputCancel?.();
+ };
const listener: NativeInputListener = {
- onTouchStart: (id, x, y) => pointer.start(id, x, y),
- onTouchMove: (id, x, y) => pointer.move(id, x, y),
- onTouchEnd: (id) => pointer.end(id),
- onTouchCancel: (id) => pointer.cancel(id),
+ onTouchStart: (id, x, y) => { heldTouches.add(id); pointer.start(id, x, y); },
+ onTouchMove: (id, x, y) => { if (heldTouches.has(id)) pointer.move(id, x, y); },
+ onTouchEnd: (id) => { if (heldTouches.delete(id)) pointer.end(id); },
+ onTouchCancel: (id) => { if (heldTouches.delete(id)) pointer.cancel(id); },
// Straight through, NOT via createPrimaryPointer: a mouse already is
// the pointer and carries a button, and routing it through the touch
// synthesizer would drop every button but the first.
- onPointerDown: (button, x, y) => callbacks.onPointerDown(button, x, y),
+ onPointerDown: (button, x, y) => { heldButtons.add(button); callbacks.onPointerDown(button, x, y); },
onPointerMove: (x, y) => callbacks.onPointerMove(x, y),
- onPointerUp: (button) => callbacks.onPointerUp(button),
+ onPointerUp: (button) => { if (heldButtons.delete(button)) callbacks.onPointerUp(button); },
onWheel: (dx, dy) => callbacks.onWheel(dx, dy),
- onKeyDown: (code) => callbacks.onKeyDown(code),
- onKeyUp: (code) => callbacks.onKeyUp(code),
+ onKeyDown: (code) => { heldKeys.add(code); callbacks.onKeyDown(code); },
+ onKeyUp: (code) => { if (heldKeys.delete(code)) callbacks.onKeyUp(code); },
+ };
+ const unregisterInput = this.bridge_.registerInput(listener);
+ const unregisterHide = this.bridge_.onHide?.(cancelInput);
+ this.inputCleanup_ = () => {
+ unregisterInput();
+ unregisterHide?.();
+ cancelInput();
};
- this.inputCleanup_ = this.bridge_.registerInput(listener);
}
unbindInputEvents(): void {
diff --git a/sdk/tests/native-adapter.test.ts b/sdk/tests/native-adapter.test.ts
index d843b50e2..27bc2c75b 100644
--- a/sdk/tests/native-adapter.test.ts
+++ b/sdk/tests/native-adapter.test.ts
@@ -16,6 +16,7 @@ import {
} from '../src/platform/native';
import { getPlatformType, isNative, platformLanguage } from '../src/platform';
import type { InputEventCallbacks } from '../src/platform/types';
+import { InputState, inputEventCallbacks } from '../src/input/input';
function makeBridge() {
const store = new Map<string, string>();
@@ -164,6 +165,33 @@ describe('NativePlatformAdapter (mock bridge)', () => {
adapter.unbindInputEvents();
expect(env.listeners.length).toBe(0);
});
+ it('hiding the native host cancels input and resets the primary finger', () => {
+ let hide = () => {};
+ const offHide = vi.fn();
+ env.bridge.onHide = callback => { hide = callback; return offHide; };
+ const input = new InputState(); adapter.bindInputEvents(inputEventCallbacks(input));
+ const sink = env.listeners[0];
+ sink.onKeyDown('KeyA'); sink.onPointerDown(2, 0, 0); sink.onTouchStart(1, 10, 20);
+ hide();
+ expect(input.keysDown.size).toBe(0); expect(input.touches.size).toBe(0);
+ expect(input.mouseButtons.size).toBe(0);
+ input.beginFixedStep(); expect(input.isMouseButtonPressed(0)).toBe(false); input.endFixedStep();
+ sink.onTouchStart(2, 30, 40); sink.onTouchEnd(1);
+ expect(input.isMouseButtonDown(0)).toBe(true);
+ expect(input.getMousePosition()).toEqual({ x: 30, y: 40 });
+ adapter.unbindInputEvents();
+ expect(input.touches.size).toBe(0); expect(input.mouseButtons.size).toBe(0);
+ expect(offHide).toHaveBeenCalledOnce();
+ });
+ it('rebinding native input clears the previous input state', () => {
+ const first = new InputState(); adapter.bindInputEvents(inputEventCallbacks(first));
+ env.listeners[0].onKeyDown('KeyA'); env.listeners[0].onTouchStart(1, 10, 20);
+ const next = new InputState(); adapter.bindInputEvents(inputEventCallbacks(next));
+ expect(first.keysDown.size).toBe(0); expect(first.touches.size).toBe(0);
+ expect(first.isMouseButtonDown(0)).toBe(false);
+ env.listeners[0].onTouchStart(2, 30, 40);
+ expect(next.isMouseButtonDown(0)).toBe(true); adapter.unbindInputEvents();
+ });
it('fails loud on the offscreen DOM surfaces (native uses loadImagePixels)', () => {
expect(() => adapter.createCanvas(1, 1)).toThrow(/native/);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 4c4de0b46..790ff8daf 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): reset native input when the host hides':
+ { note: 'Native input resets when the host hides.' },
'fix(input): preserve gestures when touch IDs are reused':
{ note: 'Reused touch IDs preserve both gestures.' },
'fix(input): cancel touch state when mini-games hide':