Limited availability
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The Keyboard
interface of the Keyboard API provides functions that retrieve keyboard layout maps and toggle capturing of key presses from the physical keyboard.
A list of valid code values is found in the UI Events KeyboardEvent code Values spec.
EventTarget Keyboard Instance propertiesAlso inherits properties from its parent interface, EventTarget
.
Also inherits methods from its parent interface, EventTarget
.
Keyboard.getLayoutMap()
Experimental
Returns a Promise
that resolves with an instance of KeyboardLayoutMap
which is a map-like object with functions for retrieving the strings associated with specific physical keys.
Keyboard.lock()
Experimental
Returns a Promise
that resolves after enabling the capture of key presses for any or all of the keys on the physical keyboard.
Keyboard.unlock()
Experimental
Unlocks all keys captured by the lock()
method and returns synchronously.
The following example demonstrates how to get the location- or layout-specific string associated with the key that corresponds to the 'W' key on an English QWERTY keyboard.
if (navigator.keyboard) {
const keyboard = navigator.keyboard;
keyboard.getLayoutMap().then((keyboardLayoutMap) => {
const upKey = keyboardLayoutMap.get("KeyW");
window.alert(`Press ${upKey} to move up.`);
});
} else {
// Do something else.
}
Keyboard locking
The following example captures the W, A, S, and D keys, call lock()
with a list that contains the key code attribute value for each of these keys:
navigator.keyboard.lock(["KeyW", "KeyA", "KeyS", "KeyD"]);
This captures these keys regardless of which modifiers are used with the key press. Assuming a standard United States QWERTY layout, registering KeyW
ensures that W, Shift+W, Control+W, Control+Shift+W, and all other key modifier combinations with W are sent to the app. The same applies to for KeyA
, KeyS
, and KeyD
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4