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,043
  • 1
  • 33
  • 32
George Kendros
  • 363
  • 3
  • 12
  • Looks to me like a thing a Windows API hook could do. Other than this, display stuff isn't well handled by default. See [this](http://social.technet.microsoft.com/Forums/windows/en-US/8a9b5aa7-fe33-4e6d-b39b-8ac80a21fdc2/disable-monitor-off-detection-how) for inspiration. – Doktoro Reichard Aug 23 '13 at 00:46
  • What version of Windows? – BillP3rd Aug 23 '13 at 01:03
  • it's Windows 8. – George Kendros Aug 23 '13 at 10:41
  • it's not going to be a simple script but it's possible. see http://stackoverflow.com/questions/5981520/detect-external-display-being-connected-or-removed-under-windows-7 or http://msdn.microsoft.com/en-us/library/windows/hardware/ff568431(v=vs.85).aspx – Colin Pickard Aug 23 '13 at 11:31
  • 3
    Unfortunately, it looks like there is probably [no Windows event on monitor connection](http://stackoverflow.com/questions/3267722/getting-an-event-on-monitor-hotplug-for-windows) which makes it impossible to create a scheduler task. Therefore, it would have to be a third-party program, but I have yet to find one. The only leads seem to be programming resources, which unfortunately as usual, tempts me to write such a program. `¬_¬` – Synetech Jan 01 '14 at 03:50

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.

  • Strange ... I got `wParam 7` and `lParam 0` for _both_ monitor connected and monitor disconnected ... oh well. – pepoluan Dec 17 '20 at 06:31
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.

DharmaTurtle
  • 191
  • 1
  • 7