Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Keyboard

Keyboard

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

TheKeyboard interface of theKeyboard 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 theUI Events KeyboardEvent code Values spec.

EventTarget Keyboard

Instance properties

Also inherits properties from its parent interface,EventTarget.

Instance methods

Also inherits methods from its parent interface,EventTarget.

Keyboard.getLayoutMap()Experimental

Returns aPromise that resolves with an instance ofKeyboardLayoutMap which is a map-like object with functions for retrieving the strings associated with specific physical keys.

Keyboard.lock()Experimental

Returns aPromise 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 thelock() method and returns synchronously.

Example

Keyboard mapping

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.

js
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 theW,A,S, andD keys, calllock() with a list that contains the key code attribute value for each of these keys:

js
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, registeringKeyW ensures thatW,Shift+W,Control+W,Control+Shift+W, and all other key modifier combinations withW are sent to the app. The same applies to forKeyA,KeyS, andKeyD.

Specifications

Specification
Keyboard Map
# keyboard-interface
Keyboard Lock
# keyboard-interface

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp