use-hid
Talk to human-interface devices with the WebHID API.
| Package | @vizejs/composable/use-hid |
| Own the source | vize lib pull composable:use-hid |
| Runtime exports | useHID |
| Gzip budget | 2304 B |
Minimal setup
Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).
vp install @vizejs/composable
Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.
import { useHID } from "@vizejs/composable/use-hid";
Try it
Connect a HID device
Ask for access to a keyboard-like device such as a macro keypad and list the devices already allowed.
What to observe: Choose a device opens the browser's HID chooser where WebHID exists; allowed devices are listed below.
Pass vendor and product filters to requestDevice. onInputReport receives reports after open. Every example renders a neutral message until it mounts, so server and client markup match, and then explains what to do when the browser lacks the API.
Compare the initial and interacted states


Copy the example
Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.
<script setup lang="ts">
import { useHID } from "@vizejs/composable/use-hid";
import { useMounted } from "@vizejs/composable/use-mounted";
const mounted = useMounted();
const { supported, devices, requestDevice } = useHID();
</script>
<template>
<div class="composable-example">
<p>Connect a USB or Bluetooth HID device, such as a macro keypad.</p>
<button type="button" :disabled="!mounted || !supported" @click="() => requestDevice([])">
Choose a device
</button>
<ul aria-label="Connected devices">
<li v-for="item in devices" :key="`${item.vendorId}-${item.productId}`">
{{ item.productName }}
</li>
</ul>
<output aria-live="polite">{{
!mounted
? "Checking browser support…"
: supported
? `${devices.length} devices allowed.`
: "WebHID is not available in this browser."
}}</output>
</div>
</template>
Runtime contract
| Utility | Category | Stability | SSR | Hydration | Cleanup | Targets | Host globals | Uses |
|---|---|---|---|---|---|---|---|---|
useHID |
system | experimental | deterministic-fallback | caller-managed | reactive-scope, returned-value | web, desktop | window |
tryOnScopeDispose |
API
useHID
Talk to human-interface devices with the WebHID API. devices lists granted devices as plain snapshots and follows the host's connect / disconnect events. Devices opened through open() forward inputreport events to onInputReport. Every action resolves to a boolean or value and records failures in error. Report listeners are removed and opened devices closed when the owning reactive scope stops; outside a scope call close() per device. Server rendering: supported is false and devices is empty. Inside a component the host is resolved after mounting, so hydration renders this server state first.
function useHID(options: UseHIDOptions = {}): HIDControls
const hid = useHID({ onInputReport: ({ reportId, data }) => read(reportId, data) });
const [device] = await hid.requestDevice([{ vendorId: 0x054c }]);
if (device) await hid.open(device);
Types
HIDDeviceFilter
Device filter for requestDevice.
| Member | Type | Description |
|---|---|---|
vendorId? |
number |
USB vendor id. |
productId? |
number |
USB product id. |
usagePage? |
number |
HID usage page. |
usage? |
number |
HID usage. |
HIDDeviceLike
Minimal HIDDevice used by useHID.
| Member | Type | Description |
|---|---|---|
opened |
boolean |
Whether the device is open. |
vendorId |
number |
USB vendor id. |
productId |
number |
USB product id. |
productName |
string |
Product name. |
open |
() => Promise<void> |
Open the device. |
close |
() => Promise<void> |
Close the device. |
sendReport |
(reportId: number, data: BufferSource) => Promise<void> |
Send an output report. |
sendFeatureReport |
(reportId: number, data: BufferSource) => Promise<void> |
Send a feature report. |
receiveFeatureReport |
(reportId: number) => Promise<DataView> |
Read a feature report. |
HIDHost
navigator.hid-like capability used by useHID.
| Member | Type | Description |
|---|---|---|
getDevices |
() => Promise<HIDDeviceLike[]> |
Devices the page already has access to. |
requestDevice |
(options: { readonly filters: HIDDeviceFilter[] }) => Promise<HIDDeviceLike[]> |
Prompt the user for devices. |
HIDDeviceInfo
Plain, reactive-friendly description of a granted device.
| Member | Type | Description |
|---|---|---|
device |
HIDDeviceLike |
The live device, for passing back to the actions. |
vendorId |
number |
USB vendor id. |
productId |
number |
USB product id. |
productName |
string |
Product name. |
opened |
boolean |
Whether the device was open when the list was refreshed. |
HIDInputReport
An input report received from an open device.
| Member | Type | Description |
|---|---|---|
device |
HIDDeviceLike |
Device that sent the report. |
reportId |
number |
Report id (0 when the device does not use report ids). |
data |
DataView |
Report payload. |
UseHIDOptions
Options for useHID.
| Member | Type | Description |
|---|---|---|
host? |
MaybeRefOrGetter<HIDHost | null | undefined> |
navigator.hid-like capability. |
onInputReport? |
(report: HIDInputReport) => void |
Receives input reports from every device opened through HIDControls.open. |
HIDControls
Reactive state and actions returned by useHID.
| Member | Type | Description |
|---|---|---|
supported |
ComputedRef<boolean> |
Whether WebHID is available. |
devices |
Readonly<ShallowRef<readonly HIDDeviceInfo[]>> |
Granted devices, refreshed on connect / disconnect, open and close. |
error |
Readonly<ShallowRef<unknown>> |
Most recent failure. |
requestDevice |
(filters?: HIDDeviceFilter[]) => Promise<readonly HIDDeviceLike[]> |
Prompt the user for devices (requires user activation). |
getDevices |
() => Promise<readonly HIDDeviceInfo[]> |
Refresh devices. |
open |
(device: HIDDeviceLike) => Promise<boolean> |
Open a device and start forwarding its input reports. |
close |
(device: HIDDeviceLike) => Promise<boolean> |
Stop forwarding reports and close a device. |
sendReport |
( device: HIDDeviceLike, reportId: number, data: BufferSource, ) => Promise<boolean> |
Send an output report. |
sendFeatureReport |
( device: HIDDeviceLike, reportId: number, data: BufferSource, ) => Promise<boolean> |
Send a feature report. |
receiveFeatureReport |
( device: HIDDeviceLike, reportId: number, ) => Promise<DataView | undefined> |
Read a feature report. |