HIDDevice: sự kiện inputreport

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.

Sự kiện inputreport của giao diện HIDDevice được kích hoạt khi nhận được báo cáo mới từ thiết bị HID.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.

js
addEventListener("inputreport", (event) => { })

oninputreport = (event) => { }

Loại sự kiện

Một HIDInputReportEvent. Kế thừa từ Event.

Event HIDInputReportEvent

Thuộc tính sự kiện

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

HIDInputReportEvent.data Read only

Một DataView chứa dữ liệu từ báo cáo đầu vào, không bao gồm reportId nếu giao diện HID sử dụng ID báo cáo.

HIDInputReportEvent.device Read only

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

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.

Ví dụ

Ví dụ sau minh họa cách lắng nghe sự kiện 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 Kết nối với các thiết bị HID không phổ biến.

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-hiddevice-oninputreport

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