Skip to contentGo to cookie choices
Skip to docs navigation

Mouse, keyboard and touch control pitfalls

https://docs.zokogames.com/guides/input-controls

This guide lists the mouse, keyboard, touch and gamepad bugs that most often break web games inside the portal's frame, and how to avoid each one. Your game runs in an iframe on a page that also has scrollbars, buttons and an ad overlay, so input behaves differently from a standalone page. Examples that use SDK assume const SDK = window.ZokoGames.SDK; as in the SDK introduction.

Page scrolling#

Arrow keys, space and the mouse wheel scroll the page that hosts your game unless you cancel them. Prevent the default for those events (outside text fields). See the snippet in technical requirements.

Focus#

Keys only reach your game if the game's frame has focus. After an ad, a dialog or a click on the portal's UI, focus may have moved. Give the canvas focus at start and again whenever an ad ends (the portal's ad overlay sits on top of your game and can take focus):

JavaScript
const SDK = window.ZokoGames.SDK;
await SDK.init();
canvas.tabIndex = 0;
canvas.focus();

async function playAd() {
  await SDK.ad.requestAd("rewarded", {
    adStarted: pauseAll,
    adFinished: () => {},
    adError: () => {},
  });
  resumeAll();
  canvas.focus();                    // input works again without a click
}
window.addEventListener("focus", resume);

In local mode the "ad" is a black overlay for three seconds, which is enough to test that focus comes back.

Also clear held keys on blur so a character does not keep walking:

JavaScript
window.addEventListener("blur", () => keys.clear());

Key codes#

Use event.code (physical key) for movement, so WASD works on non-QWERTY layouts, and event.key for text. Offer rebinding if you can.

Mouse#

  • Right-click shows the browser menu. Call preventDefault on contextmenu if you use it.
  • Use pointer events (pointerdown, pointermove, pointerup) instead of separate mouse and touch handlers, and call setPointerCapture for drags.
  • Pointer lock needs a user gesture and the sandbox allows it. Handle the player pressing Escape: pointer lock exits and your game should pause.
  • Do not rely on mouseleave; the cursor can leave the iframe while a button is held.

Touch#

The mobile guide covers orientation, safe areas and memory in more depth.

  • Do not detect mobile by "ontouchstart" in window. Laptops report touch. Use SDK.user.systemInfo.device.type ("desktop", "tablet" or "mobile"; read it after await SDK.init(), and fall back to your own check when SDK.environment === "disabled", where reading it throws sdkDisabled).
  • Set touch-action: none on the canvas to stop the browser panning or zooming.
  • Provide touch targets of at least 44 px, away from screen edges where the OS has gestures.
  • Support multi-touch deliberately: track touches by identifier.
  • Do not require hover.
  • On iOS, resume audio inside a gesture handler.

Text input#

Use a real <input> for text and the on-screen keyboard. Canvas-drawn fields do not bring up the keyboard on mobile. When the keyboard opens the visual viewport shrinks; make sure the field stays visible.

Gamepads#

If you support gamepads, use the Gamepad API and show the right prompts. The portal frame allows gamepad access.

Fullscreen and orientation#

The portal supplies a fullscreen button and a rotate prompt. Do not add your own fullscreen toggle or lock orientation in code.

Test checklist#

  • Hold a key, click elsewhere, return: nothing is stuck.
  • Wheel and arrows never scroll the page.
  • Right-click never opens the browser menu in-game.
  • On a phone: no accidental zoom, no text selection, no pull-to-refresh.
  • After an ad, input works without clicking first.

Next steps#

Something unclear, wrong or missing on this page? Tell us through support · Test your game in the preview tool