Back to Content

HID: disconnect event

files/en-us/web/api/hid/disconnect_event/index.md

latest1.3 KB
Original Source

{{securecontext_header}}{{APIRef("WebHID API")}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_worker_except_shared")}}

The disconnect event of the {{domxref("HID")}} interface fires when the user agent disconnects a HID device.

Syntax

Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.

js-nolint
addEventListener("disconnect", (event) => { })

ondisconnect = (event) => { }

Event type

A {{domxref("HIDConnectionEvent")}}. Inherits from {{domxref("Event")}}.

{{InheritanceDiagram("HIDConnectionEvent")}}

Event properties

In addition to the properties listed below, properties from the parent interface, {{domxref("Event")}}, are available.

  • {{domxref("HIDConnectionEvent.device", "device")}} {{ReadOnlyInline}}
    • : The {{domxref("HIDDevice")}} the event is fired for.

Examples

In the following example an event listener is registered to listen for the disconnection of a device. The name of the device is then printed to the console using {{domxref("HIDDevice.productName")}}.

js
navigator.hid.addEventListener("disconnect", ({ device }) => {
  console.log(`HID disconnected: ${device.productName}`);
});

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}