【问题标题】:How to start an infinity loop on AutoHotKey when reloading the script?重新加载脚本时如何在 AutoHotKey 上启动无限循环?
【发布时间】:2017-04-20 05:55:53
【问题描述】:

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

加载 AutoHotKey 脚本后,我想启动一个看门狗来监控 Octave 的绘图窗口何时打开。如果是这样,脚本将最大化窗口。

但是,当我重新加载此脚本时,它不会启动,即不显示 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

【问题讨论】:

  • 当我测试你的脚本时,“嗨”在我重新加载时再次出现。我通过右键单击 AutoHotKey 任务栏图标并选择“编辑此脚本”来测试您的脚本。我用你的替换了我的默认脚本并保存了它。然后我再次右键单击任务栏图标并选择“重新加载此脚本”。 “嗨”突然响起。当我右键单击任务栏图标并再次选择“重新加载此脚本”时,“嗨”再次弹出。
  • 你贴的代码前后有代码吗?
  • 是的,感谢您的尝试。我会将其添加到问题中。我的 AutoHotKey 版本是 1.0.48.05
  • 您的代码似乎以 NumpadDot:: 开头。并返回。 AHK 将自动启动任何加载的脚本,直到它遇到的第一个 Return。一旦 AHK 找到返回,所有其他命令(在第一次返回之后)仅在触发时执行(例如热键)。
  • 我会使用一个计时器来启动标记脚本(SetTimer [, Label, Period|On|Off|Delete, Priority]),而不是睡眠。所以不要循环,而是每 1000 毫秒启动一次带标签的脚本。

标签: autohotkey


【解决方案1】:

@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 1AutoHotKey 应用程序开始使用大约 0.08% 我的 CPU 处理能力,这要好得多。

#persistent

SetTitleMatchMode 1
Loop
{
    WinMaximize Figure 1
    Sleep 1000
}

Return

至于@Robert Ilbrink评论:

您的代码似乎以 NumpadDot:: 开头。并返回。 AHK 将自动启动任何加载的脚本,直到它遇到的第一个 Return。一旦 AHK 找到返回,所有其他命令(在第一次返回之后)仅在触发时执行(例如热键)。

我会使用一个计时器来启动标记脚本(SetTimer [, Label, Period|On|Off|Delete, Priority]),而不是睡眠。所以不要循环,而是每 1000 毫秒启动一次带标签的脚本。

#persistent

SetTimer, check_for_windows, 1000
SetTitleMatchMode 1

check_for_windows:
{
    ; MsgBox, Hello
    WinMaximize Figure 1
}

Return

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

参考资料:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2012-07-23
    • 2015-02-06
    相关资源
    最近更新 更多