Run a script when second monitor is connected

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.


Solution 1:

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.