API Docs

Module for monitoring USB device connections and disconnections

Properties

This module has no properties.

Methods

hs.usb.attachedDevices() -> [[String: Any]]

Returns all currently attached USB devices.

hs.usb.attachedDevices() -> [[String: Any]]
[[String: Any]]
An array of objects describing each attached USB device. Each object has productName (string), vendorName (string), productID (number), and vendorID (number). serialNumber (string) and locationID (number) are included when available.
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 any The event to listen for
listener any Called when a matching device event occurs
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 any The event the listener was registered for
listener any The function originally passed to on
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 any The event to listen for
listener any Called the next time a matching device event occurs, then automatically removed
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)
}