KeyboardLayoutMap:forEach() 方法

Limited availability

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

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

KeyboardLayoutMap 接口的 forEach() 方法针对映射的每个元素执行一次提供的函数。

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

语法

js
forEach(callbackFn)
forEach(callbackFn, thisArg)

参数

callbackFn

对每个元素执行的函数,接受三个参数:

currentValue

当前正在处理的元素的值。

index 可选

当前正在处理的元素的索引。

array 可选

正在调用 forEach() 的 KeyboardLayoutMap。

thisArg 可选

执行 callback 时用作 this 的值(即引用 Object)。

返回值

无(undefined)。

示例

以下示例迭代英文 QWERTY 键盘上每个位置或布局特定的字符串及其关联的键盘代码。

js
navigator.keyboard.getLayoutMap().then((keyboardLayoutMap) => {
  keyboardLayoutMap.forEach((key, code) => {
    console.log(`键盘代码 ${code} 表示 ${key}`);
  });
});

规范

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

浏览器兼容性

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
forEach
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.

参见