重新加载脚本时如何在 AutoHotKey 上启动无限循环?

How to start an infinity loop on AutoHotKey when reloading the script?

如何在重新加载脚本时在 AutoHotKey 上启动无限循环?

加载我的 AutoHotKey 脚本后,我想启动一个看门狗来监视 Octave 的 Plot window 何时打开。如果是这样,脚本将最大化 window.

然而,当我重新加载此脚本时,它没有启动,即不显示 Hi 消息框。

#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

这是脚本之前的代码:

NumpadDot::.
Return

F1::F2
Return

RCtrl::RAlt
Return

#Persistent ; uncomment this line to see the effect
SetTimer, Hello, 1000 ; go to lable hello every second

Hello:
MsgBox Hi
Return


#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

;
^+8::
Run "D:\User\Documents\AutoHotKey\MyBatches\kill_macro_player.vbs"

参考文献:

  1. https://autohotkey.com/docs/commands/SetTitleMatchMode.htm
  2. https://autohotkey.com/docs/commands/WinWait.htm
  3. https://autohotkey.com/docs/commands/WinMaximize.htm
  4. https://autohotkey.com/docs/commands/_Persistent.htm

@Jim U 评论之后,我发现要解决此问题,将脚本添加为文件中的第一件事:

#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

但是,AutoHotKey 应用程序开始使用我的 CPU 的大约 2%,而不是 0.00% 与脚本中的这些行之前一样。然后我只用 WinMaximize Figure 1 替换了 WinWait, Figure 1 并且 AutoHotKey 应用程序开始使用我的 [=63] 的 0.08% =]的处理能力,要好很多。

#persistent

SetTitleMatchMode 1
Loop
{
    WinMaximize Figure 1
    Sleep 1000
}

Return

至于@Robert Ilbrink评论:

It looks like your code starts with NumpadDot::. and Return. AHK will autostart any loaded script UP TO the first Return it encounters. As soon as AHK finds a return, all other commands (after that first return) are only executed when triggered (e.g. Hotkey).

Instead of Sleep, I would use a timer that launches a labeled script (SetTimer [, Label, Period|On|Off|Delete, Priority]). So don't loop, but just launch your labeled script every 1000 ms.

#persistent

SetTimer, check_for_windows, 1000
SetTitleMatchMode 1

check_for_windows:
{
    ; MsgBox, Hello
    WinMaximize Figure 1
}

Return

但是 SleepSetTimer 的两种方式 CPU 使用保持相同的方式,但 SetTimer 似乎消耗了 0.09% 而不是我 CPU 使用的 0.08%

参考文献:

  1. https://autohotkey.com/docs/commands/SetTimer.htm
  2. https://autohotkey.com/board/topic/40986-what-is-wintext/
  3. https://autohotkey.com/board/topic/63955-can-you-please-give-a-simple-example-of-persistent/

总处理程序

我创建了一个更通用的脚本处理程序,现在可以为更多程序设置它。在这里,我评论了 WinWait 命令,因为它们吃得太多 CPU 如上所述。

; Declare it to be alive all the time.
#persistent

; Create the initial task to start working.
SetTimer, check_for_sublime_settings_window, 1000
SetTimer, check_for_octave_graphics_window, 1000

; Set to window's title must start with the specified WinTitle to be a match.
SetTitleMatchMode 1

; Stop the initial configuration setup, i.e., this execution flow.
Return

check_for_sublime_settings_window:
{
    ; Here we wait until the windows is activated and only then to maximize it.
    ;WinWait, Preferences.sublime-settings

    ; If we do not wait for it, it will bring the windows up every time we minimize it.
    WinMaximize, Preferences.sublime-settings
    
    ; Set to wait for the next time the windows open within 1 seconds delay.
    SetTimer, check_for_sublime_settings_window, 1000
}
; To stop this execution flow
Return


check_for_octave_graphics_window:
{
    ;WinWait, Figure 1
    WinMaximize, Figure 1
    SetTimer, check_for_octave_graphics_window, 1000
}
Return