Autohotkey 启动/停止按键上的循环仅偶尔起作用

Autohotkey 启动/停止按键上的循环仅偶尔起作用,autohotkey,Autohotkey,我想知道我的代码是否有问题。我试着用F2来编写切换代码。它做垃圾邮件的工作相当不错,但当我再次按F2键将其关闭时,它只是偶尔工作。不知道是否垃圾邮件干扰了我的F2键,这会使它掉下来。下面是代码。另外,我可以得到一个修饰符,使代码可以运行在一个单独的窗口上的背景 F2:: Toggle := !Toggle loop { If not Toggle { Send, {LCtrl Up} break } Send, {LCtrl Down} s

我想知道我的代码是否有问题。我试着用F2来编写切换代码。它做垃圾邮件的工作相当不错,但当我再次按F2键将其关闭时,它只是偶尔工作。不知道是否垃圾邮件干扰了我的F2键,这会使它掉下来。下面是代码。另外,我可以得到一个修饰符,使代码可以运行在一个单独的窗口上的背景

F2::
Toggle := !Toggle
loop
{ If not Toggle
    { 
    Send, {LCtrl Up}   
      break
    }
   Send, {LCtrl Down}
   sleep, 200
   Send, {LCtrl Up}  
}
Return
另一种方法:

; autoexecute-section (top of the script):
loop_enabled := false ; the loop is disabled by default

#MaxThreadsPerHotkey 2 ; allows us to invoke F2 even if the previous invocation of F2 hasn't completed.

; Press F2 down to enable the loop (the $ symbol facilitates the "P" mode of GetKeyState below).
$F2:: loop_enabled := true

#If (loop_enabled) ; If you enable the loop by pressing F2 down

    $F2 Up::  ; release F2 to start the loop
        ToolTip, Looping
        Loop
        {
            Send, {LCtrl Down}
            Loop 20
            {
                If !GetKeyState("F2", "P") 
                    sleep, 10
            }
            Send, {LCtrl Up}
            If GetKeyState("F2", "P") ; by pressing F2 while the loop is enabled
            {
              Send, {Blind}{LCtrl Up}
              ToolTip
              loop_enabled := false      ; disable and
                    break                ; terminate the loop
            }
        }
    Return

#If

你的脚本还可以。之所以会出现这种情况,是因为循环控制了流程,而在这些时刻,热键没有响应——设计上也是如此。
对于这个应用程序,我将把整个应用程序写成一个循环。这是有点不紧凑,但它应该是稳健的,并明确了到底发生了什么

下面是一个工作示例。
要调整重复的速度-更改
T\u按
T\u释放
常数。
您还可以更改
sleep 10
,以提高/降低循环速度

Toggle := 0
ticks := 0          ; 'clock' counter

R_prev := getkeystate("F2")

T_press := 4            ; ticks before  Ctrl press
T_release := 40     ; ticks before Ctrl release  

tooltip,  Paused

loop
{ 
    sleep 10            ; loop interval
    R := getkeystate("F2") 
    RX := (R > R_prev)
    R_prev := R
    if RX {
        if Toggle {
            Toggle := 0
            ticks := 0
            send {LCtrl up}  
            tooltip,  Paused
        } else {
            Toggle := 1
        }
    }

    if Toggle {
        ticks += 1
        if (ticks = T_press) {
            send {LCtrl down}
            ; tooltip,  Pressed -- %ticks%
        }
        if (ticks = T_release) {
            send {LCtrl up}  
            ; tooltip,  Released -- %ticks%     
            ticks := 0
        }
        tooltip,  ctrl_on: %pressed% --- %ticks%
    }
}

另一个好方法是使用

计时器被证明是可中断的,因此立即停止计时器应该没有问题。 对于您的任务-诀窍是设置两个具有相同周期的计时器,并始终以偏移量开始它们。 (请参见
sleep 120
行)。这和期限可以根据您的需要进行调整

settimer, fire_stop, 400
settimer, fire, 400
settimer, fire, off
settimer, fire_stop, off

Toggle := 0

F2::
    Toggle := !Toggle
    if Toggle {
        settimer, fire_stop, on
        sleep 120                   ; offset between timers
        settimer, fire, on
        gosub fire
    } else {
        settimer, fire, off
        settimer, fire_stop, off
        gosub fire_stop
    }
return

fire:
    tooltip  "key is down"  
    send {LCtrl down}
return

fire_stop:
    tooltip  "key is up" 
    send {LCtrl up}
return

我只是测试一下,它似乎更好。我需要详细了解它的工作原理,但big Thank将“睡眠200”改为“睡眠20x10 ms”,以使休息更快、更可靠。相关: