A Human Interface Device (HID) is a type of device that takes input from or provides output to humans. It also refers to the HID protocol, a standard for bi-directional communication between a host and a device that is designed to simplify the installation procedure. The HID protocol was originally developed for USB devices but has since been implemented over many other protocols, including Bluetooth.
Interfaces
-
HID
-
Provides methods for connecting to HID devices, listing attached HID devices and event handlers for connected HID devices.
-
HIDDevice
-
Represents an HID device. It's possible for a single physical device to be represented by multiple HIDDevice
objects.
-
HIDInputReportEvent
-
Passed to HIDDevice.inputreport_event
when an input report is received from any associated HID device.
-
HIDConnectionEvent
-
Passed to HID
connect
and disconnect
events when a device is connected or disconnected.
Examples
You can connect to a device with the requestDevice()
method. In this case, we select from all the available devices.
const device = await navigator.hid.requestDevice({ filters: [] });
We can retrieve all the devices the website has been granted access to previously and log the device names to the console.
let devices = await navigator.hid.getDevices();
devices.forEach((device) => {
console.log(`HID: ${device.productName}`);
});
We can register event listeners for disconnection of any HID devices.
navigator.hid.addEventListener("disconnect", (event) => {
console.log(`HID disconnected: ${event.device.productName}`);
console.dir(event);
});
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
WebHID_API |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
connect_event |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
disconnect_event |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
getDevices |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
requestDevice |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |