API Docs

Module for monitoring USB device connections and disconnections

Object Types

Plain JavaScript objects that this module's methods and properties take or return:

HSUSBDevice

An attached USB device

Field Type Description
productName string The device's product name, or "" if it doesn't have one
vendorName string The device's manufacturer, or "" if it doesn't have one
productID number The USB product ID
vendorID number The USB vendor ID
serialNumber optional string The device's serial number, if it has one
locationID optional number Where the device is attached, if known

Event Types

The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:

HSUSBEvent

An event name for hs.usb.on(), hs.usb.off() and hs.usb.once()

Event Description
"added" A USB device was connected
"removed" A USB device was disconnected

Properties

This module has no properties.

Methods

hs.usb.attachedDevices() -> HSUSBDevice[]

Returns all currently attached USB devices.

hs.usb.attachedDevices() -> HSUSBDevice[]
The attached USB devices
const devices = hs.usb.attachedDevices()
devices.forEach(d => console.log(d.vendorName + " " + d.productName))

hs.usb.on(event, listener) -> None

Register a listener for USB device connection and disconnection events.

hs.usb.on(event, listener) -> None
Name Type Description
event HSUSBEvent The event to listen for
listener (device: HSUSBDevice) => void Called when a matching device event occurs
Event Description
"added" A USB device was connected
"removed" A USB device was disconnected
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.usb.on('added', device => console.log(device.vendorName + " " + device.productName + " connected"))
    hs.usb.on('removed', device => console.log(device.vendorName + " " + device.productName + " removed"))
} catch (err) {
    console.error(err.message)
}

hs.usb.off(event, listener) -> None

Remove a previously registered USB device event listener.

hs.usb.off(event, listener) -> None
Name Type Description
event HSUSBEvent The event the listener was registered for
listener (device: HSUSBDevice) => void The function originally passed to on
Event Description
"added" A USB device was connected
"removed" A USB device was disconnected
None
const onAdded = device => console.log(device.productName)
hs.usb.on('added', onAdded)
// later…
hs.usb.off('added', onAdded)

hs.usb.once(event, listener) -> None

Register a listener that fires at most once for a USB device event.

hs.usb.once(event, listener) -> None
Name Type Description
event HSUSBEvent The event to listen for
listener (device: HSUSBDevice) => void Called the next time a matching device event occurs, then automatically removed
Event Description
"added" A USB device was connected
"removed" A USB device was disconnected
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.usb.once('added', device => console.log("First device seen: " + device.productName))
} catch (err) {
    console.error(err.message)
}