KeyboardLayoutMap:has() 方法

Limited availability

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

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

KeyboardLayoutMap 接口的 has() 方法返回一个布尔值,指示该对象是否具有指定键的元素。

UI 事件 KeyboardEvent 代码值规范中可以找到有效键的列表。

该方法与 Map.prototype.has() 相似。

语法

js
has(key)

参数

key

要在映射中搜索的元素的键。

返回值

Boolean 值,表示是否找到了指定的键。

示例

以下示例检查与英语 QWERTY 键盘上“W”键对应的键盘代码关联的位置或布局特定的字符串是否存在。

js
navigator.keyboard.getLayoutMap().then((keyboardLayoutMap) => {
  console.log(keyboardLayoutMap.has("KeyW"));
});

规范

Specification
ECMAScript® 2025 Language Specification
# sec-map.prototype.has

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
has
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

参见