web api interface
HIDDevice
Secure contextLimited availabilityAvailable in workers
The HIDDevice interface of the WebHID 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.
Instance properties
This interface also inherits properties from EventTarget.
openedRead only Experimental- : Returns a
Boolean, true if the device has an open connection.
- : Returns a
vendorIdRead only Experimental- : Returns the vendorId of the HID device.
productIdRead only Experimental- : Returns the productId of the HID device.
productNameRead only Experimental- : Returns a string containing the product name of the HID device.
collectionsRead only Experimental- : 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 from EventTarget.
open()Experimental- : Opens a connection to this HID device, and returns a
Promisewhich resolves once the connection has been successful.
- : Opens a connection to this HID device, and returns a
close()Experimental- : Closes the connection to this HID device, and returns a
Promisewhich resolves once the connection has been closed.
- : Closes the connection to this HID device, and returns a
forget()Experimental- : Closes the connection to this HID device and resets access permission, and returns a
Promisewhich resolves once the permission was reset.
- : Closes the connection to this HID device and resets access permission, and returns a
sendReport()Experimental- : Sends an output report to this HID Device, and returns a
Promisewhich resolves once the report has been sent.
- : Sends an output report to this HID Device, and returns a
sendFeatureReport()Experimental- : Sends a feature report to this HID device, and returns a
Promisewhich resolves once the report has been sent.
- : Sends a feature report to this HID device, and returns a
receiveFeatureReport()Experimental
Examples
The following example demonstrates listening for an inputreport event that will allow the application to detect which button is pressed on a Joy-Con Right device.
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 example sendFeatureReport is used to make a device blink.
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 article Connecting to uncommon HID devices.
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.