hs.usb
ModuleModule 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.
Declaration
hs.usb.attachedDevices() -> [[String: Any]]
Returns
[[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.Example
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.
Declaration
hs.usb.on(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called when a matching device event occurs |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
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.
Declaration
hs.usb.off(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event the listener was registered for |
| listener | any | The function originally passed to on |
Returns
None
Example
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.
Declaration
hs.usb.once(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called the next time a matching device event occurs, then automatically removed |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
try {
hs.usb.once('added', device => console.log("First device seen: " + device.productName))
} catch (err) {
console.error(err.message)
}