这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

lock() 方法在 键盘 interface returns a Promise after enabling the capture of keypresses for any or all of the keys on the physical keyboard. This method can only capture keys that are granted access by the underlying operating system.

句法

var promise = Keyboard.lock([keyCodes[]])
					

参数

keyCodes 可选
数组 of one or more key codes to lock. If no keycodes are provided all keys will be locked. A list of valid code values is found in the UI Events KeyboardEvent code Values spec.

返回值

A Promise .

范例

Capturing all keys

The following example captures all keypresses.

navigator.keyboard.lock();
					

Capturing specific keys

The following example captures the "W", "A", "S", and "D" keys. It captures these keys regardless of which modifiers are used with the key press. Assuming a standard US 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" .

navigator.keyboard.lock(["KeyW", "KeyA", "KeyS", "KeyD"]);
					

规范

规范 状态 注释
Keyboard Lock
The definition of 'lock()' in that specification.
编者草案 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
lock Chrome 68 Edge 79 Firefox No IE No Opera 55 Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

元数据

  • 最后修改:
  1. 键盘
  2. 方法
    1. getLayoutMap()
    2. lock()
    3. unlock()