JavaScript Navigator keyboard Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

navigator.keyboard is a read-only property that returns a Keyboard object — the entry point for layout maps and Keyboard Lock (MDN). Use it to map physical keys to characters and optionally capture key presses.

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use navigator.keyboard. Start with this line.

JavaScript
navigator.keyboard
CallWhat it does
navigator.keyboardPrimary signature.

Built into the browser. Read navigator.keyboard on Navigator — no import required.

Library declarations
JavaScript
navigator.keyboard

Parameters

ParameterDescription
lock()Optional. Capture listed keys (or all)
unlock()Optional. Release all locks (sync)

Return Value

Return ValueDescription
*Keyboard object

JavaScript Examples

Example 1 — Feature-Detect

Check whether the Keyboard API is available before calling it.

JavaScript
const supported = "keyboard" in navigator;
console.log(supported ? "Keyboard API available" : "Keyboard API missing");
Try It Yourself

How It Works

  1. Use in. "keyboard" in navigator checks for the property.
  2. Expect limited support. Strongest historically in Chromium.
  3. Secure context matters. Missing also means HTTP or blocked policy.
  4. Keep a fallback. Use normal KeyboardEvent listeners when unsupported.

Example 2 — getLayoutMap()

Find which character the physical KeyW key produces on this layout.

JavaScript
async function labelKeyW() {
  if (!("keyboard" in navigator)) {
    console.log("unsupported");
    return;
  }
  const map = await navigator.keyboard.getLayoutMap();
  const upKey = map.get("KeyW");
  console.log("Press " + upKey + " to move up.");
}

labelKeyW();
Try It Yourself

How It Works

  1. Detect first. Exit early when keyboard is missing.
  2. Await getLayoutMap(). Resolves to a KeyboardLayoutMap (MDN).
  3. Look up a code. map.get("KeyW") returns the layout string for that physical key.
  4. Show friendly UI. Games can say “Press Z…” on AZERTY instead of hard-coding W.

Example 3 — lock() / unlock()

Capture WASD from a button click. Browsers often also require JavaScript-initiated fullscreen.

JavaScript
async function lockWasd() {
  if (!("keyboard" in navigator) || !navigator.keyboard.lock) {
    console.log("unsupported");
    return;
  }
  try {
    await navigator.keyboard.lock(["KeyW", "KeyA", "KeyS", "KeyD"]);
    console.log("WASD locked");
  } catch (err) {
    console.log("An error occurred.");
  }
}

function unlockKeys() {
  if (navigator.keyboard && navigator.keyboard.unlock) {
    navigator.keyboard.unlock();
    console.log("unlocked");
  }
}

document.querySelector("#lock").addEventListener("click", lockWasd);
document.querySelector("#unlock").addEventListener("click", unlockKeys);
Try It Yourself

How It Works

  1. Wire a click handler. MDN requires transient user activation for lock().
  2. Pass key codes. Empty args lock all keys; listed codes lock only those.
  3. Expect fullscreen rules. Many Chromium builds only allow lock after JS fullscreen.
  4. Call unlock(). Releases every lock synchronously (MDN).

Browser Support

Navigator.keyboard is Experimental and Limited availability (not Baseline) on MDN. It requires a secure context. Permissions Policy may block layout map access.

✓ Experimental · Not Baseline

Navigator.keyboard

Support is strongest in Chromium-based browsers (Chrome ~68+, Edge 79+, Opera). Firefox and Safari typically omit the Keyboard API. Always feature-detect.

Limited Not Baseline
Google Chrome 68+ Keyboard API (secure context)
Supported
Microsoft Edge 79+ Chromium Edge — available
Supported
Opera 55+ Chromium-based — generally available
Supported
Mozilla Firefox Not typically available
Unavailable
Apple Safari Not typically available
Unavailable
navigator.keyboard Limited

Bottom line: Detect over HTTPS, use getLayoutMap for labels, lock only when needed from a gesture, and keep normal key events as the fallback.

Frequently Asked Questions

It is a read-only Navigator property that returns a Keyboard object — the entry point for keyboard layout maps (getLayoutMap) and Keyboard Lock (lock / unlock) so apps can capture physical key presses (MDN).
Yes. MDN marks it Experimental and Limited availability (not Baseline). Always feature-detect and fall back to normal KeyboardEvent listeners.
Yes. It is available only in secure contexts (HTTPS or localhost) in supporting browsers.
keyboard.getLayoutMap() returns a Promise that resolves to a KeyboardLayoutMap. You look up physical key codes like "KeyW" to learn which character that key produces on the user’s layout (useful for AZERTY, QWERTZ, and other layouts).
It captures key presses for listed physical keys (or all keys) so your page can receive them even when the browser would normally handle them. Call unlock() to release. MDN requires transient user activation; browsers often also need a privileged context such as JavaScript-initiated fullscreen.
No. It is read-only. You use methods on the returned Keyboard object; you do not assign to navigator.keyboard.

Next: language

Learn navigator.language for the user’s preferred language.

language tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful