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.addWatcher(listener) -> None
Register a listener for USB device connection and disconnection events.
The listener is called with two arguments: the event type string (`"added"` or `"removed"`) and a device-info object with the same fields as `attachedDevices()`.
Declaration
hs.usb.addWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | JSValue | The function to call when a USB device is added or removed |
Returns
None
Example
const handler = (event, device) => {
console.log(event + ": " + device.productName + " by " + device.vendorName)
}
hs.usb.addWatcher(handler)
hs.usb.removeWatcher(listener) -> None
Remove a previously registered USB event listener.
Declaration
hs.usb.removeWatcher(listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| listener | JSValue | The function originally passed to `addWatcher` |
Returns
None
Example
const handler = (event, device) => console.log(event + ": " + device.productName)
hs.usb.addWatcher(handler)
// later…
hs.usb.removeWatcher(handler)