Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HIDDevice

HIDDevice

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 insecure contexts (HTTPS), in some or allsupporting browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Note: This feature is available inWeb Workers, except forShared Web Workers.

TheHIDDevice interface of theWebHID API represents a HID Device. It provides properties for accessing information about the device, methods for opening and closing the connection, and the sending and receiving of reports.

EventTarget HIDDevice

Instance properties

This interface also inherits properties fromEventTarget.

HIDDevice.openedRead onlyExperimental

Returns aboolean, true if the device has an open connection.

HIDDevice.vendorIdRead onlyExperimental

Returns the vendorId of the HID device.

HIDDevice.productIdRead onlyExperimental

Returns the productId of the HID device.

HIDDevice.productNameRead onlyExperimental

Returns a string containing the product name of the HID device.

HIDDevice.collectionsRead onlyExperimental

Returns an array of report formats for the HID device.

Events

inputreportExperimental

Fires when a report is sent from the device.

Instance methods

This interface also inherits methods fromEventTarget.

HIDDevice.open()Experimental

Opens a connection to this HID device, and returns aPromise which resolves once the connection has been successful.

HIDDevice.close()Experimental

Closes the connection to this HID device, and returns aPromise which resolves once the connection has been closed.

HIDDevice.forget()Experimental

Closes the connection to this HID device and resets access permission, and returns aPromise which resolves once the permission was reset.

HIDDevice.sendReport()Experimental

Sends an output report to this HID Device, and returns aPromise which resolves once the report has been sent.

HIDDevice.sendFeatureReport()Experimental

Sends a feature report to this HID device, and returns aPromise which resolves once the report has been sent.

HIDDevice.receiveFeatureReport()Experimental

Receives a feature report from this HID device in the form of aPromise which resolves with aDataView. This allows typed access to the contents of this message.

Examples

The following example demonstrates listening for aninputreport event that will allow the application to detect which button is pressed on a Joy-Con Right device.

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]}.`);});

In the following examplesendFeatureReport is used to make a device blink.

js
const reportId = 1;for (let i = 0; i < 10; i++) {  // Turn off  await device.sendFeatureReport(reportId, Uint32Array.from([0, 0]));  await new Promise((resolve) => setTimeout(resolve, 100));  // Turn on  await device.sendFeatureReport(reportId, Uint32Array.from([512, 0]));  await new Promise((resolve) => setTimeout(resolve, 100));}

You can see more examples, and live demos in the articleConnecting to uncommon HID devices.

Specifications

Specification
WebHID API
# dom-hiddevice

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp