aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 69d91e857d6c512ec6aa0a18cfbf2b82ec2856bc
parent 3097e5e97b9b5e7534ce31de0dc0ed4327a745cf
Author: anon <anon@example.com>
Date:   Fri, 23 Oct 2026 12:49:08 +0000

Keep Tab targeting focused in the WASM game

Diffstat:
MTODO.md | 2+-
Mclient/browser/index.html | 3+++
Mclient/tests/browser_controls.mjs | 35+++++++++++++++++++++++++++++++++++
3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/TODO.md b/TODO.md @@ -487,7 +487,7 @@ state, SQLite records and the owner/observer UI; record reproducible steps and expected results. Include fresh characters and existing characters, reconnects and server restarts. Turn every reproduced bug into a focused regression test. -- [ ] Capture Tab reliably while the WASM game has keyboard focus. Check: +- [x] Capture Tab reliably while the WASM game has keyboard focus. Check: repeated Tab presses cycle player/NPC/monster targets without moving focus to Chrome's address bar; text fields and controls outside the game retain normal keyboard navigation. Verify in Chrome with manual keyboard input. diff --git a/client/browser/index.html b/client/browser/index.html @@ -814,6 +814,9 @@ show(); event.preventDefault(); closeChat(true); } + } else if (gameReady && document.activeElement === canvas && event.key === 'Tab') { + // Keep target cycling in the game while SDL receives the original key. + event.preventDefault(); } else if (gameReady && keypadKeys[event.code]) { event.preventDefault(); event.stopImmediatePropagation(); diff --git a/client/tests/browser_controls.mjs b/client/tests/browser_controls.mjs @@ -115,6 +115,41 @@ function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149. hold: () => { for (const callback of timers.values()) callback(); timers.clear(); }}; } +test('Tab stays in the focused game and still reaches native key handlers', () => { + const game = page(); + const delivered = []; + game.window.addEventListener('keydown', event => delivered.push([event.key, !!event.shiftKey, !!event.repeat])); + + for (const values of [{}, {shiftKey: true}, {repeat: true}]) { + for (const type of ['keydown', 'keyup']) { + const event = new Event(type, {cancelable: true}); + Object.assign(event, {key: 'Tab', code: 'Tab'}, values); + game.window.dispatchEvent(event); + assert.equal(event.defaultPrevented, true); + } + } + + assert.deepEqual(delivered, [['Tab', false, false], ['Tab', true, false], ['Tab', false, true]]); + assert.deepEqual(game.keys, []); +}); + +test('Tab retains normal navigation outside the ready game canvas', () => { + for (const scenario of ['loading', 'options', 'chat', 'button']) { + const game = page({ready: scenario !== 'loading'}); + if (scenario === 'options') game.fire('#menu-button', 'click'); + if (scenario === 'chat') game.fire('#chat-button', 'click'); + if (scenario === 'button') game.element('#menu-button').focus(); + game.keys.length = 0; + const event = new Event('keydown', {cancelable: true}); + Object.assign(event, {key: 'Tab', code: 'Tab'}); + + game.window.dispatchEvent(event); + + assert.equal(event.defaultPrevented, false, scenario); + assert.deepEqual(game.keys, [], scenario); + } +}); + test('physical keypad keys survive NumLock aliases and keypad Enter remains distinct', () => { const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}}); const events = [