AutoHotKey:MonitorCount 作为触发器?

AutoHotKey: MonitorCount as trigger?

我希望我的 AutoHotKey 脚本不是由键盘快捷键触发,而是由事件触发,即每当我 attach/detach 第二台显示器时。我写了这个正确确定和显示监视器数量的小脚本,但只有当我手动 运行 脚本时才会这样做:

SysGet, MonitorCount, MonitorCount
if (MonitorCount<>lastMonitorCount)
   {
      MsgBox, Monitor Count:`t%MonitorCount%
   }
SysGet, lastMonitorCount, MonitorCount

如何让 MsgBox 在活动监视器的数量发生变化时自动出现?

设置一个计时器,每当连接的显示器数量发生变化时,该计时器就会弹出消息框:

#Persistent                          ; prevents the script from exiting when it reaches the 'Return'
SetTimer, DetectMonitorCount, 500    ; check every 500 milliseconds

    DetectMonitorCount:              ; this is a label
SysGet, MonitorCount, MonitorCount
if (MonitorCount <> lastMonitorCount)
    MsgBox, Monitor Count:`t%MonitorCount%
SysGet, lastMonitorCount, MonitorCount
Return                               ; ends the subroutine

https://autohotkey.com/docs/commands/SetTimer.htm

看来我找到了答案:

while 2>1
{
  SysGet, MonitorCount, MonitorCount
  if (MonitorCount<>lastMonitorCount)
     {
        MsgBox, Monitor Count:`t%MonitorCount%
     }
  SysGet, lastMonitorCount, MonitorCount
}

是的,您可以 SetTimer 或循环……但 OnMessage 是更好的解决方案。

此 post 中的更多详细信息:https://superuser.com/questions/635526/run-a-script-when-second-monitor-is-connected/