HIDDevice:collections 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

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

注意:此功能在 Web Workers 中可用,但 共享 Web Workers 除外。

HIDDevice 接口的只读属性 collections 返回一个报告格式数组

报告格式数组。每个条目包含以下内容:

usagePage

一个整数,表示与此集合关联的 HID 用法的 usage page 组件。顶级集合的用法用于识别设备类型。

标准 HID 用法值可以在 HID Usage Tables 文档中找到。

usage

一个整数,表示与此集合关联的 HID 用法的 usage ID 组件。

type

一个 8 位值,表示集合类型,它描述了分组项之间不同的关系。以下是其中一种:

0x00

Physical (group of axes)

0x01

Application (mouse, keyboard)

0x02

Logical (interrelated data)

0x03

Report

0x04

Named array

0x05

Usage switch

0x06

Usage modified

0x070x7F

Reserved for future use

0x800xFF

Vendor-defined

有关这些类型的更多信息可以在 Device Class Definition 文档中找到。

children

子集合的数组,其格式与顶级集合相同。

inputReports

inputReport 条目的数组,表示此集合中描述的单个输入报告。

outputReports

outputReport 条目的数组,表示此集合中描述的单个输出报告。

featureReports

featureReport 条目的数组,表示此集合中描述的单个功能报告。

示例

以下示例演示了在返回 collections 属性后如何访问各种元素。您可以在文章 Connecting to uncommon HID devices 中找到更多示例和实时演示。

js
for (const collection of device.collections) {
  // A HID collection includes usage, usage page, reports, and subcollections.
  console.log(`Usage: ${collection.usage}`);
  console.log(`Usage page: ${collection.usagePage}`);

  for (const inputReport of collection.inputReports) {
    console.log(`Input report: ${inputReport.reportId}`);
    // Loop through inputReport.items
  }

  for (const outputReport of collection.outputReports) {
    console.log(`Output report: ${outputReport.reportId}`);
    // Loop through outputReport.items
  }

  for (const featureReport of collection.featureReports) {
    console.log(`Feature report: ${featureReport.reportId}`);
    // Loop through featureReport.items
  }

  // Loop through subcollections with collection.children
}

规范

规范
WebHID API
# dom-hiddevice-collections

浏览器兼容性