9

I currently have a TV attached to my computer via HDMI. When I set the TV's input to the appropriate HDMI port, it becomes visible to the computer and the computer adds it as a second display.

What I wanted to do is run a script (to launch XBMC) when the secondary monitor is connected.

OS is Windows 8.

Mat
  • 8,353

2 Answers2

4

You can do this in AutoHotKey or AutoIt. They can hook WinApi, pretty easy, create a .ahk file and run it with AutoHotkeyU64.exe

OnMessage(0x219, "MsgMonitor")
MsgMonitor(wParam, lParam, msg)
{
    if (wParam = 7) {
        Run, Notepad.exe
    } Else {
        MsgBox probably disconected. do something else
    }
    MsgBox check %wParam% and %lParam% and decide to run programs with %msg%
}
;wParam: 7 lParam: 0  monitor connected
;wParam: 32772 lParam: 8977536 should be on disconected

I dont have any HDMI devices i can test it, but works when i am disconnecting my DVI cable from main monitor.

0

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.