--- title: Keyboard slug: Web/API/Keyboard translation_of: Web/API/Keyboard ---
{{SeeCompatTable}}{{APIRef("Keyboard API")}}

The Keyboard interface of the 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.

Properties

None.

Methods

{{domxref('Keyboard.getLayoutMap()')}} {{experimental_inline}}
Returns a {{jsxref('Promise')}} that resolves with an instance of {{domxref('KeyboardLayoutMap')}} which is a map-like object with functions for retrieving the strings associated with specific physical keys.
{{domxref('Keyboard.lock()')}} {{experimental_inline}}
Returns a {{jsxref('Promise')}} after enabling the capture of keypresses for any or all of the keys on the physical keyboard.
{{domxref('Keyboard.unlock()')}} {{experimental_inline}}
Unlocks all keys captured by the lock() method and returns synchronously.

Example

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) {
  var keyboard = navigator.keyboard;
  keyboard.getLayoutMap()
  .then(keyboardLayoutMap => {
    var upKey = keyboardLayoutMap.get('KeyW');
    window.alert('Press ' + upKey + ' to move up.');
  });
} else {
  // Do something else.
}

Specifications

Specification Status Comment
{{SpecName('Keyboard Map','#keyboard-interface','Keyboard')}} {{Spec2('Keyboard Map')}} Initial definition.
{{SpecName('Keyboard Lock','#keyboard-interface','Keyboard')}} {{Spec2('Keyboard Lock')}} Adds lock() and unlock().

Browser compatibility

{{Compat("api.Keyboard")}}