AutoHotKey:MonitorCount作为触发器?

时间:2019-02-17 15:43:59

标签: autohotkey

我希望我的AutoHotKey脚本不是由键盘快捷键触发,而是由事件触发,即每当我连接/分离第二台监视器时触发。我写了这个小脚本,可以正确地确定并显示监视器的数量,但是只有当我手动运行该脚本时:

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

只要活动监视器的数量发生变化,如何使MsgBox自动出现?

2 个答案:

答案 0 :(得分:3)

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

#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

答案 1 :(得分:0)

好像我找到了答案:

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