05-03-26
USB Device Trigger Script
This Lua script for Rebind hardware input triggers a macro when a specific USB device is connected. It uses system commands to detect the device and offers configuration options for device name, check
Question
I need a script to trigger a macro when a specific USB device is connected.
Rebind AI
Here's a script that detects when a specific USB device is connected and triggers a macro:
-- rebind: name=USB Device Macro Trigger
-- rebind: description=Trigger macros based on connected USB devices
local cfg = UI.Schema({
enabled = UI.Toggle(true, { label = "Enable" }),
device_name = UI.Text("", {
label = "USB Device Name",
placeholder = "e.g., 'Controller', 'Gamepad', 'Keyboard'",
maxLength = 100,
}),
trigger_key = UI.Keybind("F9", { label = "Manual Trigger Key" }),
check_interval = UI.Slider(1000, {
min = 500,
max = 5000,
suffix = "ms",
label = "Check Interval",
}),
})
local macro = {
{ x = 0, y = 2, delay = 15 },
{ x = 1, y = 3, delay = 15 },
{ x = 0, y = 2, delay = 15 },
{ x = -1, y = 1, delay = 15 },
}
local deviceConnected = false
local checkTimer = nil
local function checkDevice()
if not cfg.enabled or cfg.device_name == "" then
return false
end
-- Execute a command to list connected USB devices
local result = System.Exec("wmic logicaldisk get name", { timeout = 2000 })
-- Check if device name appears in output (simple substring match)
-- For more reliable detection, customize this based on your device
local found = result.stdout:lower():find(cfg.device_name:lower()) ~= nil
if found and not deviceConnected then
deviceConnected = true
UI.Notify("Device connected: " .. cfg.device_name, "success")
Log.Info("USB device detected, triggering macro")
Macro.Play(macro, 1.0, "replace")
return true
elseif not found and deviceConnected then
deviceConnected = false
UI.Notify("Device disconnected", "warning")
Log.Info("USB device removed")
return false
end
return found
end
function OnStart()
Log.Info("USB Device Trigger ready")
-- Start periodic device checking
checkTimer = Timer.Every(cfg.check_interval, function()
checkDevice()
end)
end
function OnStop()
if checkTimer then
checkTimer:Cancel()
end
end
Bind(cfg.trigger_key, function()
if checkDevice() then
UI.Notify("Manual trigger: macro played", "info")
else
UI.Notify("Device not found", "warning")
end
return false
end)How It Works
-
Periodic Polling – Uses
Timer.Every()to check for the device at configurable intervals (default 1 second) -
Device Detection – Executes
wmic logicaldisk get nameto query connected devices and searches for your device name substring -
State Tracking – Tracks whether the device was previously connected to detect connect/disconnect events
-
Macro Trigger – When the device is detected, plays the macro immediately
-
Manual Override – Press the configured hotkey to manually trigger the macro and check device status
Configuration
- Device Name: Enter a substring of your USB device name (case-insensitive matching)
- Check Interval: How often to poll for the device (1000ms = 1 second)
- Manual Trigger Key: Button to manually check and trigger if device is present
For More Reliable Detection
Replace the System.Exec() call with a more specific Windows command:
-- List all USB devices with friendly names
local result = System.Exec("wmic path Win32_USBControllerDevice get Dependent", { timeout = 2000 })
-- Or check device manager output
local result = System.Exec("devcon status *", { timeout = 2000 })
-- Or use PowerShell for detailed device info
local result = System.Exec("powershell -Command \"Get-PnpDevice -Class USB\"", { timeout = 3000 })