Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

User Activation, Idle Detection, Wake Lock, and Keyboard Web APIs

How HTML user activation gates powerful APIs, plus idle detection, screen wake locks, and keyboard control on the web platform.

Browsers treat user activation as a security and ergonomics boundary: many APIs run only after real input, and separate mechanisms report when someone goes idle or when a page must keep the screen on or manage keyboards. This page summarizes the WHATWG HTML model of tracking user activation and the related capabilities documented for Chromium-based browsers in Chrome for Developers, including extension-only APIs where the open web offers a different surface.

User activation: why interaction gates APIs

Section titled “User activation: why interaction gates APIs”

User activation exists so pages cannot abuse features that annoy users without a deliberate gesture.

To prevent abuse of certain APIs that could be annoying to users (e.g., opening popups or vibrating phones), user agents allow these APIs only when the user is actively interacting with the web page or has interacted with the page at least once. This “active interaction” state is maintained through the mechanisms defined in this section.

— WHATWG, HTML: 6.4 Tracking user activation

The developer-oriented edition of the same section describes the same rules in plainer layout; behavior matches the full specification.

Activation-gated API levels: sticky, transient, and consuming

Section titled “Activation-gated API levels: sticky, transient, and consuming”

APIs that depend on user activation fall into four levels. The distinction that matters in practice is whether activation persists after the gesture, whether multiple calls are allowed before it expires, and whether each call consumes the activation.

Level Requires Consumes activation? Typical implication
Sticky activation-gated Sticky activation (true after first user activation) No Blocked until the user has activated the page at least once
Transient activation-gated Transient activation No Several calls allowed until transient activation expires
Transient activation-consuming Transient activation Yes, per call Only one use per gesture (e.g. opening a popup)
History-action activation-consuming History-action activation Yes, per history-action activation One use per qualifying history-related gesture

APIs that are dependent on user activation are classified into different levels:

Sticky activation-gated APIs — These APIs require the sticky activation state to be true, so they are blocked until the very first user activation.

Transient activation-gated APIs — These APIs require the transient activation state to be true, but they don’t consume it, so multiple calls are allowed per user activation until the transient state expires.

Transient activation-consuming APIs — These APIs require the transient activation state to be true, and they consume user activation in each call to prevent multiple calls per user activation.

History-action activation-consuming APIs — These APIs require the history-action activation state to be true, and they consume history-action user activation in each call to prevent multiple calls per user activation.

— WHATWG, HTML: 6.4 Tracking user activation

If a call fails with a security error or requires a user gesture, check which gate applies in the specification for that API and whether your code runs in a setTimeout, promise chain, or cross-origin frame after the click or key event.

Idle Detection API: when the user stops interacting

Section titled “Idle Detection API: when the user stops interacting”

Websites that need coarse “away” or “locked screen” signals use the Idle Detection API, not to be confused with the extension browser.idle API, which reports OS-level idle timing for extensions.

The web API notifies developers when a user is idle according to browser heuristics:

The Idle Detection API notifies developers when a user is idle, indicating such things as lack of interaction with the keyboard, mouse, screen, activation of a screensaver, locking of the screen, or moving to a different screen. A developer-defined threshold triggers the notification.

— Google Chrome, Detect inactive users with the Idle Detection API

The API models two dimensions:

The user idle state: active or idle: the user has or has not interacted with the user agent for some period of time.

The screen idle state: locked or unlocked: the system has an active screen lock (like a screensaver) preventing interaction with the user agent.

— Google Chrome, Detect inactive users with the Idle Detection API

Granularity is intentionally coarse for privacy; the spec does not require separating activity in one tab from activity elsewhere—that is left to the user agent.

Permission, gesture, and IdleDetector.start()

Section titled “Permission, gesture, and IdleDetector.start()”

Use requires the idle-detection permission, a secure top-level context, and IdleDetector.requestPermission(), which needs a user gesture:

const state = await IdleDetector.requestPermission();
if (state !== 'granted') {
return;
}
const idleDetector = new IdleDetector();
idleDetector.addEventListener('change', () => {
console.log(idleDetector.userState, idleDetector.screenState);
});
await idleDetector.start({ threshold: 60000, signal });

The minimum threshold is 60,000 ms (one minute). Stop detection with an AbortSignal as described in Detect inactive users with the Idle Detection API.

Extension browser.idle vs web IdleDetector

Section titled “Extension browser.idle vs web IdleDetector”
Surface Purpose Typical caller
browser.idle.queryState() Returns "locked", "idle", or "active" after a configurable interval without user input Browser extensions
IdleDetector Fires change when user/screen idle state crosses a threshold (≥ 1 minute) Secure websites with permission

Returns “locked” if the system is locked, “idle” if the user has not generated any input for a specified number of seconds, or “active” otherwise.

— Google Chrome, browser.idle

Page scripts cannot replace extension idle APIs for cross-tab OS idle; polyfills that only listen inside one document cannot see idle on other tabs, as noted in Detect inactive users with the Idle Detection API.

Screen Wake Lock API: keep the display on during tasks

Section titled “Screen Wake Lock API: keep the display on during tasks”

Recipes, tours, kiosks, and presentations often need the screen to stay lit without native apps. The Screen Wake Lock API exposes a single lock type, screen.

The Screen Wake Lock API provides a way to prevent the device from dimming and locking the screen. This capability enables new experiences that, until now, required a platform-specific app.

— Google Chrome, Stay awake with the Screen Wake Lock API

Request and release:

let wakeLock = null;
try {
wakeLock = await navigator.wakeLock.request();
} catch (err) {
console.error(err.name, err.message);
}
// later: wakeLock.release();

Locks require HTTPS. The browser may refuse requests (for example low battery). An earlier system wake lock type in the specification is not proceeding—only screen remains:

Caution: An earlier version of the specification allowed an additional system wake lock that prevents the device’s CPU from entering standby mode… We aren’t proceeding with this type.

— Google Chrome, Stay awake with the Screen Wake Lock API

Wake locks release when the tab becomes hidden; re-request on visibilitychange when the document is visible again, as in Stay awake with the Screen Wake Lock API. Prefer wake locks only when needed; for downloads or sync, consider background fetch or background sync as lighter alternatives on the same page.

Extension browser.power vs navigator.wakeLock

Section titled “Extension browser.power vs navigator.wakeLock”

Extensions can call browser.power.requestKeepAwake(level) with "system" or "display":

The “system” level keeps the system active, but allows the screen to be dimmed or turned off… The “display” level keeps the screen and system active.

— Google Chrome, browser.power

When multiple extensions hold levels, "display" wins over "system". Websites use navigator.wakeLock instead; extensions use browser.power—different security models and platforms.

Keyboard Lock API: immersive full-screen input

Section titled “Keyboard Lock API: immersive full-screen input”

Games, remote desktop streaming, and full-screen apps may need OS-captured keys (for example Esc, Alt+ Tab). The Keyboard Lock API works with JavaScript-initiated full screen, not user-shortcut full screen alone:

The Keyboard Lock API is only available when JavaScript-initiated full screen is active.

— Google Chrome, Capture Keys with the Keyboard Lock API

await document.documentElement.requestFullscreen();
await navigator.keyboard.lock(['KeyW', 'KeyA', 'KeyS', 'KeyD']);
// navigator.keyboard.unlock();

From Chrome 130 onward, keyboard (and pointer) lock may require permission; see Chrome’s permission article linked from Capture Keys with the Keyboard Lock API. Browsers must offer an escape hatch even if all keys are locked—for example in Chrome, a long (two-second) Esc press exits keyboard lock, as described under security considerations on that page.

Virtual Keyboard API: layout when the on-screen keyboard appears

Section titled “Virtual Keyboard API: layout when the on-screen keyboard appears”

On phones and tablets, the Virtual Keyboard API lets apps handle occlusion instead of relying solely on the browser’s default scroll-into-view behavior:

Traditionally, browsers have dealt with this challenge on their own, but more complex applications may require more control over the browser’s behavior.

— Google Chrome, Full control with the VirtualKeyboard API

Opt in by setting navigator.virtualKeyboard.overlaysContent = true, then use show(), hide(), boundingRect, and geometrychange events. CSS environment variables such as keyboard-inset-height adjust layout:

margin-block-end: env(keyboard-inset-height, 0px);

The virtualkeyboardpolicy attribute (auto or manual) on contenteditable hosts decouples focus from showing the keyboard, as in Full control with the VirtualKeyboard API.

IME and Enter: avoid submitting during composition

Section titled “IME and Enter: avoid submitting during composition”

Custom Enter handlers on chat or search UIs must not treat composition Enter as submit. IME-safe enter-to-submit applies to any custom keydown on Enter—not only <textarea>:

If custom JavaScript listens to keydown on Enter and submits immediately, the user’s message is sent while they are still converting characters, resulting in incomplete, fragmented, or incorrect messages.

— Google Chrome, IME-safe enter-to-submit

Guard with event.isComposing (or related composition state) before submitting. Native <form> submission from a single-line <input> without custom handlers already suppresses submit while the IME is composing.

  • Calling activation-consuming APIs after await without chaining from the user event handler.
  • Assuming IdleDetector works without permission or below the one-minute minimum threshold.
  • Holding a screen wake lock without re-acquiring after tab switches.
  • Requesting keyboard lock without requestFullscreen() or without an exit path for users.
  • Submitting on Enter during IME composition.
  1. In DevTools, trigger the user gesture first, then call the gated API synchronously or from the same event handler; confirm behavior matches HTML: 6.4 Tracking user activation.

  2. For idle handling, grant idle-detection when prompted and verify change events with thresholds ≥ 60,000 ms using the patterns in Detect inactive users with the Idle Detection API.

  3. On HTTPS, request navigator.wakeLock.request(), switch tabs, and confirm release and re-acquisition per Stay awake with the Screen Wake Lock API.

  4. In full screen, exercise Capture Keys with the Keyboard Lock API and the documented Esc long-press escape path.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.