Connecting to uncommon devices from your app.
An HID 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 HIDs by relying on these drivers.
The inability to access uncommon HID devices is particularly painful when it comes to gamepad support. Gamepads designed for PC often use HID for gamepad inputs (buttons, joysticks, triggers) and outputs (LEDs, rumble). However, 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 present in the behavior of specific devices.
If you're curious about how any of these samples work, the source code for all of them is available on GitHub. There's a barebones code example in the explainer.
The biggest barrier to trying out any of these demos is lack of access to the device. Fortunately, if you have a MacBook Pro with a TouchBar, you don't need to buy anything. This demo lets you use the API right from your laptop. It also shows how WebHID can be used to unlock functionality of built-in devices, not just peripherals.
Demo/Source: Keyboard Backlight
Next up is something fewer of you are likely to have. Sony's DualShock 4 is a wireless controller for PlayStation 4 game consoles.
The DualShock 4 Demo uses WebHID to receive the raw input reports from the DualShock 4 and provides a high-level API to access the controller's gyroscope, accelerometer, touchpad, button, and thumbstick inputs. It also supports rumble and setting the color of an RGB LED housed within the controller.
Demo: DualShock 4 Demo (Source)
Play the Chrome dino 🦖 offline game by actually jumping with a Nintendo Switch Joy-Con controller in your pants pockets. This demo is powered by Joy-Con WebHID, a WebHID driver for the Nintendo Switch Joy-Con controllers.
Author: Thomas Steiner
Demo: Chrome Dino WebHID (demo source, driver source)
BlinkStick Strip is a HID-compliant light strip with 8 RGB LEDs. The demo allows the user to select from several blink patterns including chase, blink, and Larson scanner (aka Cylon).
Author: Robat Williams
Demo: blinkstick-strip (source)
What does this demo do? (Wait for it.) It blinks. Actually it's three demos that use the blink(1) USB notification light.
blink(1) is simple and well-documented, which makes it a great option for getting started with HID.
Author: Tod E. Kurt
Demos: blink(1) (source)
Thank you to Pete LePage and Kayce Basques for reviews of this article.
Photo by Ugur Akdemir on Unsplash