I got wParam 7 and lParam 0 for both monitor connected & disconnected. So I used SysGet and MonitorCount to count the number of monitors to determine if they're being connected or disconnected.
SysGet, oldMonitorCount, MonitorCount
OnMessage(0x219, "MsgMonitor")
MsgMonitor() {
global oldMonitorCount
Sleep 5000 ; needs time to detect any new monitors
SysGet, newMonitorCount, MonitorCount
if (oldMonitorCount = newMonitorCount) {
MsgBox Some other device was (un)plugged
} else {
if (newMonitorCount = 2) {
MsgBox Docked - battle station ready for action.
} else {
MsgBox Undocked - be free!
}
}
oldMonitorCount := newMonitorCount
}
I think the above OnMessage(0x219) comes from this list of Windows messages. Notably, the code 0x0219 is associated with WM_DEVICECHANGE. This means that it'll trigger whenever any device is (un)plugged, including USB devices. For me, this included plugging in headphones. To solve this, I track the current monitor count and then only run my script if that number changes.