【问题标题】:How can I interrupt a loop by pressing "any" key in AHK?如何通过按 AHK 中的“任意”键来中断循环?
【发布时间】:2013-12-03 16:07:26
【问题描述】:

我目前有一个脚本反复按下鼠标左键,直到脚本被中断。要启动和停止脚本,请按下 ALT+2。如何让它以 ALT+2 开始,但按任意键停止?

#MaxThreadsPerHotkey 3
!2::  ; ALT+2 hotkey 
#MaxThreadsPerHotkey 1
if KeepWinZRunning  
{
    KeepWinZRunning := false  ; 
    return  ; 
}

; Otherwise:
KeepWinZRunning := true
Loop
{
    ToolTip, Press ALT+2 again to stop.
    Sleep 100
    Send, {VK01 down}{VK01 up}
    Sleep 100
    if not KeepWinZRunning  

        break  ; Break out of this loop.

}
KeepWinZRunning := false  ; Reset in preparation for the next press of this hotkey.
ToolTip
return

ExitApp
F12::ExitApp

【问题讨论】:

  • 1) 使用 timers 而不是 endless 循环,它们为(伪)多线程提供了更好的支持。 2) Input command 是您正在寻找的。不过,请仔细查看选项。
  • MCL 是对的。计时器还为您提供了同时运行线程甚至中断计时器线程的选项。 MCL 以前帮过我。

标签: autohotkey


【解决方案1】:

根据我的评论,这是一个使用 TimersInput 的示例:

endKeys={enter}{tab}{LControl}{RControl}{LAlt}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}

!2::
    SetTimer, SendSomething, 200
    Input, pressedKey, I L1, % endKeys
    SetTimer, SendSomething, Off
return

SendSomething:
    Send, {VK01 down}{VK01 up}
return

您可能需要填写endkeys 的列表,具体取决于您的键盘布局。

【讨论】:

    【解决方案2】:

    参加look at this post。这是一个直接的报价。您应该查看原始帖子以获取详细信息(我不能对此表示赞赏),但这里是 sn-p:

    #InstallKeybdHook  ; this MUST be called at the start of your script
    
    AnyKeyPressed() ; returns a 1 if any keyboard key is pressed, else returns 0
    {
        if( A_TimeIdlePhysical < 25 )
            return 1
    
    return 0
    }
    

    【讨论】:

    • 这在大多数情况下都行不通。 A_TimeIdlePhysical 也会被鼠标事件重置。因此,只要鼠标移动,循环就会停止;而不仅仅是在按下某个键时。
    • @MCL - 那么,您是说 keybdhook 将鼠标视为键盘键?
    • @MCL A_TimeIdlePhysical 不会被重置,因为#InstallKeybdHook
    • 你说的都是对的。我从来不知道如果只安装一个挂钩,那么只有它的物理输入类型会影响 A_TimeIdlePhysical
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2020-10-16
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多