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

瀏覽器相容性