docs » USBDeviceActions

Open/close apps or run an arbitrary function when a USB device is connected/disconnected.

Example configuration (using SpoonInstall.spoon):

function toggleKeyboardLayout(x)
  if x then
    hs.keycodes.setLayout("U.S.")
  else
    hs.keycodes.setLayout("Colemak")
  end
end

spoonInstall:andUse(
  "USBDeviceActions",
  {
    config = {
      devices = {
        ScanSnapiX500EE            = { apps = { "ScanSnap Manager Evernote Edition" } },
        Planck                     = { fn = toggleKeyboardLayout },
        ["Corne Keyboard (crkbd)"] = { fn = toggleKeyboardLayout }
      }
    },
    start = true
  }
)

API Overview

API Documentation

Variables

devices
Signature USBDeviceActions.devices (Table)
Type Variable
Description A table where the keys should correspond to `productName`s of USB devices and the values should be tables containing the keys `apps` and/or `fn`. (You can find the `productName` for a connected USB device using `hs.usb.attachedDevices()`).
Notes
  • The value of the apps key should contain a list of apps that will be launched/killed when the USB device is connected/disconnected. The value of the fn key should be a single parameter function that will be passed true/false when the USB device is connected/disconnected.

Example:

{
  ScanSnapiX500EE            = { apps = { "ScanSnap Manager Evernote Edition" } },
  Planck                     = { fn = toggleKeyboardLayout },
  ["Corne Keyboard (crkbd)"] = { fn = toggleKeyboardLayout }
}
Source Source/USBDeviceActions.spoon/init.lua line 87

Methods

init
Signature USBDeviceActions:init() -> Self
Type Method
Description Creates an `hs.usb.watcher` with a callback that will execute the specified actions for the USB devices in `USBDeviceActions.devices` when they are connected/disconnected, but doesn't start the watcher.
Parameters
  • None
Returns
  • Self
Source Source/USBDeviceActions.spoon/init.lua line 103
start
Signature USBDeviceActions:start() -> Self
Type Method
Description Starts the `hs.usb.watcher` created by `USBDeviceActions:init()`.
Parameters
  • None
Returns
  • Self
Source Source/USBDeviceActions.spoon/init.lua line 118
stop
Signature USBDeviceActions:stop() -> Self
Type Method
Description Stops the `hs.usb.watcher` created by `USBDeviceActions:init()`.
Parameters
  • None
Returns
  • Self
Source Source/USBDeviceActions.spoon/init.lua line 132