HIDInputReportEvent

Limited availability

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

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is available in Web Workers, except for Shared Web Workers.

Giao diện HIDInputReportEvent của WebHID API được truyền vào sự kiện inputreport của HIDDevice khi nhận được một báo cáo đầu vào từ bất kỳ thiết bị HID liên kết nào.

Event HIDInputReportEvent

Thuộc tính phiên bản

Giao diện này cũng kế thừa các thuộc tính từ Event.

HIDInputReportEvent.data Read only Experimental

Một DataView chứa dữ liệu từ báo cáo đầu vào, ngoại trừ reportId nếu giao diện HID sử dụng ID báo cáo.

HIDInputReportEvent.device Read only Experimental

Phiên bản HIDDevice đại diện cho giao diện HID đã gửi báo cáo đầu vào.

HIDInputReportEvent.reportId Read only Experimental

Tiền tố nhận dạng một byte cho báo cáo này, hoặc 0 nếu giao diện HID không sử dụng ID báo cáo.

Phương thức phiên bản

Giao diện này kế thừa các phương thức từ lớp cha, Event.

Ví dụ

Ví dụ sau đây minh họa việc lắng nghe một inputReport cho phép ứng dụng phát hiện nút nào được nhấn trên thiết bị Joy-Con Right. Bạn có thể xem thêm ví dụ và bản demo trực tiếp trong bài viết Connecting to uncommon HID devices.

js
device.addEventListener("inputreport", (event) => {
  const { data, device, reportId } = event;

  // Handle only the Joy-Con Right device and a specific report ID.
  if (device.productId !== 0x2007 && reportId !== 0x3f) return;

  const value = data.getUint8(0);
  if (value === 0) return;

  const someButtons = { 1: "A", 2: "X", 4: "B", 8: "Y" };
  console.log(`User pressed button ${someButtons[value]}.`);
});

Thông số kỹ thuật

Specification
WebHID API
# dom-hidinputreportevent

Tương thích trình duyệt