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()
Fundamentals
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.
Concept
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.
Foundation
📝 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.
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");
Keyboard API missing
(or "Keyboard API available" in a supporting browser)
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");
}
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.
LimitedNot Baseline
Google ChromeKeyboard Map / Lock in recent desktop Chrome (secure context)
Supported*
Microsoft EdgeChromium Edge — follows Chrome Keyboard API support
Supported*
OperaChromium-based — where Chromium enables Keyboard API
Supported*
Mozilla FirefoxTypically unavailable — check current MDN
Unavailable
Apple SafariTypically unavailable — check current MDN
Unavailable
keyboardLimited
Bottom line: Detect navigator.keyboard over HTTPS. Prefer event.code for games; use getLayoutMap for user-facing key labels.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about keyboard
Experimental Keyboard entry — layout maps and optional key lock.
5
Core concepts
⌨01
Returns
Keyboard object
Entry
🔒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.