首页 > 解决方案 > AutoHotKey:MonitorCount 作为触发器?

问题描述

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

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

MsgBox每当活动监视器的数量发生变化时,如何使显示自动出现?

标签: autohotkey

解决方案


Set up a timer that will pop up the message box whenever the number of attached monitors changes:

#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


推荐阅读