JavaScript Navigator keyboard Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Experimental
Secure context

What You’ll Learn

navigator.keyboard is the entry point for the Keyboard API. Learn layout maps with getLayoutMap, Keyboard Lock with lock / unlock, secure context rules, five examples, and try-it labs.

01

Kind

Read-only property

02

Returns

Keyboard object

03

Status

Experimental

04

Context

Secure (HTTPS)

05

Layout

getLayoutMap()

06

Capture

lock() / unlock()

Introduction

Games and creative apps often need two special keyboard powers: know what character a physical key produces on this user’s layout, and sometimes capture keys that the browser would normally steal (like media keys or Esc in fullscreen).

navigator.keyboard returns a Keyboard object for that. Use getLayoutMap() for layout-aware hints (“press Z to move up” on AZERTY), and lock() / unlock() for Keyboard Lock in immersive apps.

💡
Still use KeyboardEvent

Everyday typing and shortcuts still use keydown / keyup. The Keyboard API is an extra layer for layout maps and lock — not a replacement for normal event listeners.

Understanding the keyboard Property

Think of navigator.keyboard as the front desk for two related features: Keyboard Map and Keyboard Lock.

  • Keyboard object — entry point returned by navigator.keyboard.
  • getLayoutMap() — map physical codes (KeyW) to layout strings.
  • lock(keyCodes) — capture listed physical keys for your page.
  • unlock() — release all captured keys.
  • Secure context — HTTPS / localhost required in supporting browsers.

📝 Syntax

General form of the property:

JavaScript
navigator.keyboard

Value

  • A Keyboard object when the API is available.
  • Missing / unavailable when unsupported or not in a secure context.

Common patterns

JavaScript
// MDN-style layout lookup:
if (navigator.keyboard) {
  const keyboard = navigator.keyboard;
  keyboard.getLayoutMap().then((keyboardLayoutMap) => {
    const upKey = keyboardLayoutMap.get("KeyW");
    console.log("Press " + upKey + " to move up.");
  });
}

// Keyboard Lock (games / fullscreen apps):
// await navigator.keyboard.lock(["KeyW", "KeyA", "KeyS", "KeyD"]);
// navigator.keyboard.unlock();

⚡ Quick Reference

GoalCode
Get Keyboard entrynavigator.keyboard
Detect APIBoolean(navigator.keyboard)
Layout mapawait navigator.keyboard.getLayoutMap()
Label for KeyWmap.get("KeyW")
Lock WASDawait navigator.keyboard.lock(["KeyW","KeyA","KeyS","KeyD"])
Release locksnavigator.keyboard.unlock()
Secure page?window.isSecureContext
Status (MDN)Experimental / not Baseline

🔍 At a Glance

Four facts to remember about navigator.keyboard.

Returns
Keyboard

API entry

Status
experimental

Not Baseline

HTTPS
required

Secure context

Features
map + lock

Two APIs

📋 Keyboard API vs KeyboardEvent

navigator.keyboardkeydown / keyup
RoleLayout map + key captureEveryday key input
SupportExperimental / limitedUniversal
Layout aware?Yes via getLayoutMap()Use event.code / event.key
Capture browser keys?Possible with lock()Often blocked by the browser
Default for appsOptional enhancementAlways start here

Examples Gallery

Examples follow MDN Keyboard / navigator.keyboard patterns. Prefer Try It Yourself on HTTPS. Keyboard Lock may require fullscreen or other browser rules.

📚 Getting Started

Detect the API and confirm a secure context.

Example 1 — Feature Detection

Check whether the Keyboard API entry point exists.

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

How It Works

MDN’s layout example starts with if (navigator.keyboard) — same idea.

Example 2 — Secure Context Check

The Keyboard API needs HTTPS / localhost in supporting browsers.

JavaScript
console.log("secure: " + window.isSecureContext);
console.log("keyboard: " + Boolean(navigator.keyboard));

if (!window.isSecureContext) {
  console.log("Serve over HTTPS to use navigator.keyboard");
} else if (!navigator.keyboard) {
  console.log("Secure, but Keyboard API not exposed");
} else {
  console.log("Ready for getLayoutMap / lock");
}
Try It Yourself

How It Works

Treat a missing property as unsupported for this page — do not assume lock will work later.

📈 Practical Patterns

Layout maps, Keyboard Lock, and a status helper.

Example 3 — Layout Map for KeyW

MDN’s example: find the character for the physical KeyW position.

JavaScript
async function labelForKeyW() {
  if (!navigator.keyboard) {
    return "Keyboard API not supported.";
  }
  const map = await navigator.keyboard.getLayoutMap();
  const upKey = map.get("KeyW");
  return "Press " + upKey + " to move up.";
}

labelForKeyW().then(console.log);
Try It Yourself

How It Works

On QWERTY you often get w; other layouts may return a different string for the same physical key.

Example 4 — Lock WASD Keys

MDN’s Keyboard Lock sample captures W, A, S, and D for game-style movement.

JavaScript
async function lockWasd() {
  if (!navigator.keyboard || !navigator.keyboard.lock) {
    return "Keyboard Lock not supported.";
  }
  await navigator.keyboard.lock(["KeyW", "KeyA", "KeyS", "KeyD"]);
  return "Locked KeyW / KeyA / KeyS / KeyD (call unlock() when done)";
}

// Prefer calling from a user gesture / fullscreen game session.
// navigator.keyboard.unlock();
Try It Yourself

How It Works

Locking captures the key with any modifiers. Always call unlock() when leaving the game or fullscreen.

Example 5 — Capability Helper

Bundle detection for layout map and lock into one status object.

JavaScript
function keyboardStatus() {
  const kb = navigator.keyboard;
  return {
    available: Boolean(kb),
    secureContext: window.isSecureContext,
    hasGetLayoutMap: Boolean(kb && typeof kb.getLayoutMap === "function"),
    hasLock: Boolean(kb && typeof kb.lock === "function"),
    advice: kb
      ? "Use getLayoutMap for labels; lock only in immersive apps"
      : "Fall back to keydown / keyup with event.code"
  };
}

console.log(JSON.stringify(keyboardStatus()));
Try It Yourself

How It Works

Some engines may expose layout map without full Keyboard Lock — check methods separately.

🚀 Common Use Cases

  • Localized game hints — show the right key label for “move up” on every layout.
  • Fullscreen games — lock WASD / Esc handling when the browser allows it.
  • Creative tools — capture keys that would otherwise trigger browser chrome.
  • Accessibility copy — teach shortcuts using the user’s actual key glyphs.
  • Progressive enhancement — Keyboard API when present; event.code otherwise.

🧠 How navigator.keyboard Works

1

Detect in a secure context

navigator.keyboard must exist on HTTPS / localhost.

Detect
2

Read the layout map

getLayoutMap() maps codes like KeyW to strings.

Map
3

Optionally lock keys

lock() captures keys for immersive sessions.

Lock
4

Unlock and fall back

Call unlock() when done; keep keydown as the default path.

📝 Notes

  • Experimental and not Baseline — support is limited.
  • Not Deprecated or Non-standard on MDN.
  • Secure context required (HTTPS / localhost).
  • Use physical codes from the UI Events KeyboardEvent code Values list (e.g. KeyW).
  • Related: language, ink, JavaScript hub.

Limited Browser Support

navigator.keyboard is Experimental and not Baseline. Availability is limited (often Chromium-first). Always feature-detect, require HTTPS, and keep KeyboardEvent fallbacks.

Experimental · Not Baseline

Navigator.keyboard

Entry point for layout maps and Keyboard Lock. Detect the Keyboard object, use getLayoutMap for labels, and lock keys only when needed.

Limited Not Baseline
Google Chrome Keyboard Map / Lock in recent desktop Chrome (secure context)
Supported*
Microsoft Edge Chromium Edge — follows Chrome Keyboard API support
Supported*
Opera Chromium-based — where Chromium enables Keyboard API
Supported*
Mozilla Firefox Typically unavailable — check current MDN
Unavailable
Apple Safari Typically unavailable — check current MDN
Unavailable
keyboard Limited

Bottom line: Detect navigator.keyboard over HTTPS. Prefer event.code for games; use getLayoutMap for user-facing key labels.

Conclusion

navigator.keyboard is the Keyboard API entry point. Feature-detect it in a secure context, use getLayoutMap() for layout-aware labels, and reserve lock() / unlock() for immersive apps — with normal keydown listeners as your baseline.

Continue with language, ink, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect navigator.keyboard first
  • Serve over HTTPS / localhost
  • Use event.code for gameplay; layout map for UI text
  • Call unlock() when leaving fullscreen / the game
  • Keep keydown / keyup as the default path

❌ Don’t

  • Assume Safari/Firefox expose the API
  • Lock keys without a clear exit path
  • Hard-code “press W” for every locale
  • Require Keyboard Lock for core features
  • Skip explaining why you capture system keys

Key Takeaways

Knowledge Unlocked

Five things to remember about keyboard

Experimental Keyboard entry — layout maps and optional key lock.

5
Core concepts
🔒 02

HTTPS

Secure context

Required
🗺 03

Map

getLayoutMap

Labels
🔒 04

Lock

lock / unlock

Capture
🔬 05

Status

Experimental

Detect

❓ 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.
Yes. MDN marks it Experimental and not Baseline. Support is limited — 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. Locking often requires a fullscreen or similar privileged context depending on the browser.
No. It is read-only. You use methods on the returned Keyboard object; you do not assign to navigator.keyboard.
Did you know?

MDN’s WASD lock example captures those keys with any modifiers — so KeyW covers W, Shift+W, Ctrl+W, and more, not only the plain W key.

Explore language Next

Learn the preferred language tag for Intl and i18n defaults.

language →

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.

8 people found this page helpful