The WebHID API allows websites to access alternative auxiliary keyboards and exotic gamepads.
Published: September 15, 2020
There are many human interface devices (HIDs), such as alternative keyboards or exotic gamepads, that are too new, too old, or too uncommon to be accessible by systems' device drivers. The WebHID API solves this by providing a way to implement device-specific logic in JavaScript.
Suggested use cases
An HID device takes input from or provides output to humans. Examples of devices include keyboards, pointing devices (mice, touchscreens, etc.), and gamepads. The HID protocol makes it possible to access these devices on desktop computers using operating system drivers. The web platform supports HID devices by relying on these drivers.
The inability to access uncommon HID devices is particularly painful when it comes to alternative auxiliary keyboards (such as Elgato Stream Deck, Jabra headsets, X-keys) and exotic gamepad support. Gamepads designed for desktop often use HID for gamepad inputs (buttons, joysticks, triggers) and outputs (LEDs, rumble).
Unfortunately, gamepad inputs and outputs are not well standardized and web browsers often require custom logic for specific devices. This is unsustainable and results in poor support for the long tail of older and uncommon devices. It also causes the browser to depend on quirks in the behavior of specific devices.
Terminology
A human interface device (HID) can take input or offer output to humans. There's an HID protocol, a standard for bi-directional communication between a host and a device that is designed to simplify the installation procedure.
HID consists of two fundamental concepts: reports and report descriptors. Reports are the data that is exchanged between a device and a software client. The report descriptor describes the format and meaning of data that the device supports.
Applications and HID devices exchange binary data through three report types:
| Report type | Description |
|---|---|
| Input report | Data that is sent from the device to the application (e.g. a button is pressed.) |
| Output report | Data that is sent from the application to the device (e.g. a request to turn on the keyboard backlight.) |
| Feature report | Data that may be sent in either direction. The format is device-specific. |
A report descriptor describes the binary format of reports supported by the device. Its structure is hierarchical and can group reports together as distinct collections within the top-level collection. The format of the descriptor is defined by the HID specification.
An HID usage is a numeric value referring to a standardized input or output. Usage values allow a device to describe the intended use of the device and the purpose of each field in its reports. For example, one is defined for the left button of a mouse. Usages are also organized into usage pages, which provide an indication of the high-level category of the device or report.
Use the WebHID API
To check if the WebHID API is supported, use:
if ("hid" in navigator) {
// The WebHID API is supported.
}
Open an HID connection
The WebHID API is asynchronous by design to prevent the website UI from blocking when awaiting input. This is important because HID data can be received at any time, requiring a way to listen to it.
To open an HID connection, first access a HIDDevice object. For this, you can
either prompt the user to select a device by calling
navigator.hid.requestDevice(), or pick one from navigator.hid.getDevices()
which returns a list of devices the website has been granted access to
previously.
The navigator.hid.requestDevice() function takes a mandatory object that
defines filters. Those are used to match any device connected with a USB vendor
identifier (vendorId), a USB product identifier (productId), a usage page
value (usagePage), and a usage value (usage). You can get those from the
USB ID Repository and the HID usage tables document.
The multiple HIDDevice objects returned by this function represent multiple
HID interfaces on the same physical device.
// Filter on devices with the Nintendo Switch Joy-Con USB Vendor/Product IDs.
const filters = [
{
vendorId: 0x057e, // Nintendo Co., Ltd
productId: 0x2006 // Joy-Con Left
},
{
vendorId: 0x057e, // Nintendo Co., Ltd
productId: 0x2007 // Joy-Con Right
}
];
// Prompt user to select a Joy-Con device.
const [device] = await navigator.hid.requestDevice({ filters });
// Get all devices the user has previously granted the website access to.
const devices = await navigator.hid.getDevices();