Keyboard Map API

Keyboard Map API 允许获取键盘按键码至键盘按键名的映射

通过 navigator.keyboard 暴露的 Keyboard 接口实例使用

获取键盘映射

Keyboard 接口的 getLayoutMap() 方法提供了获取键盘映射的方法

方法返回一个 Promise 的 KeyboardLayoutMap 实例

1
2
3
4
navigator.keyboard.getLayoutMap().then((layoutMap) => {
const code = 'KeyW'
const key = layoutMap.get(code)
})

KeyboardLayoutMap 接口反映了一组键值对对象,键名为键盘按键码,键值为键盘按键名;它是一个只读的类 map 对象

键盘映射更改

Keyboard 接口的 layoutchange 事件在键盘映射改变时触发

该事件尚未得到主流浏览器的支持

权限策略

该 API 调用受到 keyboard-map 权限策略的控制,可以通过 Permissions-Policy 响应头指定,或通过 <iframe> 标签的 allow 属性指定

默认为 self,即允许在当前上下文或内嵌的其他同源上下文中使用

类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
interface Keyboard extends EventTarget {
lock(keyCodes?: string[]): Promise<void>
unlock(): void
}

interface Navigator {
readonly keyboard: Keyboard
}

interface KeyboardLayoutMap {
forEach(callbackfn: (value: string, key: string, map: Map<string, string>) => void, thisArg?: any): void
get(key: string): string | undefined
has(key: string): boolean
readonly size: number
entries(): IterableIterator<[string, string]>
keys(): IterableIterator<string>
values(): IterableIterator<string>
[Symbol.iterator](): IterableIterator<[string, string]>
}

链接

发布于

2023-10-28

更新于

2023-11-10

许可协议

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...