Feature Library

Keep the mouse: full screen, keyboard lock and own-key windows

Not measured yet
Still
The live demo starts when it’s in view.J notebook, M map: the same key closes it and keeps the mouse · Esc pauses · F full screen

Keeping the mouse in a browser game that plays with the pointer locked.

In a window Esc always frees it, and the page cannot take it back in Esc’s own press: Esc is never a user activation, and Chrome refuses a new lock for about a second after one. So three small parts. Full screen in one gesture: the full screen request, then the pointer lock in the same task, and the keyboard locked when full screen stands, so in Chrome and Edge a short Esc reaches the game (it pauses) and only a held Esc leaves; Firefox and Safari have no Keyboard Lock, and a line says once that Esc frees the mouse there. Windows on their own keys: the key that opens a window closes it and takes the pointer back in that same key press (a key press is an activation), Esc still closes, W, A, S or D walks away from an information window, and each footer says its key as bound (“J close · Esc”). Relock on refusal: a refused lock is not a pause; the next click or key takes the pointer back, and the line always says “click or press a key”, never “move”. A corner button for full screen shows only while the pointer is free, and one-time hints go through your own seen(key). Framework-free TypeScript, no dependencies, with a stand-in pointer lock for automated runs.

What it costs a frame

Not measured yet.

What it needs

Renders with
DOM and CSSPlain DOM and CSS: every current browser.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages
With an agent
install("keep-the-mouse")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up

What it’s for

  1. A first-person or mouse-look browser game that should not lose the mouse every time a window closes
  2. A "Play full screen" button where Esc pauses the game instead of dropping the mouse (Chrome, Edge)
  3. Inventory, map or journal windows that close on the key that opened them and hand the mouse straight back
  4. Saying the right thing when the browser refuses the pointer lock: click or press a key, once, not a pause
  5. Testing a pointer-lock game headless without the operating system clipping the real cursor

Pitfalls, in brief

  1. Esc is never a user activation.
  2. Chrome refuses a new pointer lock for about a second after the player left it with Esc, even inside a click.
  3. Mouse movement alone can never take the pointer back: the browser asks for a click or a key.
  4. The keyboard lock holds only in full screen, and only full screen asked for by the page (element.requestFullscreen): F11’s browser full screen does not count.
  5. Firefox and Safari have no Keyboard Lock API (navigator.keyboard.lock): there Esc frees the mouse in full screen too.
7 more in brief
  1. The full screen request must come first in the gesture, the pointer lock straight after in the same task.
  2. Opening a window lets go of the pointer (relock.release(), in windows’ onOpen): a window over a locked pointer cannot be clicked.
  3. A release just after a lock was asked for is no pause: Chrome can grant a lock in a closing press and drop it again. relock waits 120 ms before it calls a release a let-go, and treats one within 600 ms of lock() as a refusal.
  4. Let the registry and relock hear keys before the game does (make them first; they listen on window), so a window’s key is spent closing it and does not reach the game too.
  5. Key codes are layout-free (KeyboardEvent.code), the caps are not: on AZERTY the key at KeyM is “,”.
  6. A hidden or headless page cannot test the real pointer lock, and on Windows a headless browser that asks for it clips the real cursor to the hidden window’s rectangle.
  7. Put the target element, not the whole page, full screen when the game is one part of a page (target); a host page that puts an ancestor full screen counts as full screen too, and the keyboard is locked then as well.
12 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From another of the maker’s apps. Fully custom code.

Hannes Andersson: Flunkyard’s full screen helper, its own-key windows, the relock rules and the pointer-lock stand-ins; the library’s registry, relock helper and demo.

pointer lockmouse lookfull screenfullscreenkeyboard lockescescape keyuser activationrelockfirst persongame uigame inputwindowshotkeysown key closespauseone-time hintschromefirefoxsafariheadless testingstand-indomno dependencies

Near it in the frame