Autohotkey 如何制作完美的RoF autofire宏(睡眠问题)AHK

Autohotkey 如何制作完美的RoF autofire宏(睡眠问题)AHK,autohotkey,Autohotkey,假设我想制作一个恰好每82毫秒点击一次的宏。 我知道第一次睡眠是点击之间的延迟,但是让我们将其设置为82。 但是Lmclickup之后的延迟应该是两次点击之间的延迟。 我把它设为1,这样我的延迟时间大约为83毫秒,但实际上它偶尔会口吃,就像我把它设为100,然后按一下按钮睡觉,它就会突然着火 #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Ena

假设我想制作一个恰好每82毫秒点击一次的宏。 我知道第一次睡眠是点击之间的延迟,但是让我们将其设置为82。 但是Lmclickup之后的延迟应该是两次点击之间的延迟。 我把它设为1,这样我的延迟时间大约为83毫秒,但实际上它偶尔会口吃,就像我把它设为100,然后按一下按钮睡觉,它就会突然着火

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance Force
Process, Priority,,High
SetMouseDelay -1

~insert::Suspend

~RButton & ~LButton::
    Loop {

        Send {LButton down}
        Sleep, 82
        Send {LButton up}       
        if (GetKeyState("LButton", "P") = 0)
            break
        sleep, 1
    }
return

我建议增加睡眠

1毫秒的时间比人类点击上下所需的时间要短得多,你的游戏或你发送这些点击的任何东西可能都不能如此快速地处理输入

尝试20毫秒。

根据,Sleep命令的计时不准确:

由于操作系统计时系统的粒度,延迟通常被四舍五入到10或15.6毫秒的最接近倍数,具体取决于安装的硬件和驱动程序的类型。例如,在大多数Windows 2000/XP系统上,介于1和10之间(含1和10)的延迟相当于10或15.6。要缩短延迟,请参阅

如果CPU负载不足,实际延迟时间可能会比请求的时间长。这是因为操作系统给每个需要的进程分配了一段CPU时间,通常是在给脚本分配另一个时间片之前20毫秒

这与你的83毫秒大约为100很好地吻合。如果您遵循该示例链接,您会注意到一个示例,该示例旨在使睡眠时间短于基于系统硬件的10或15.6毫秒限制:

SetBatchLines -1  ; Ensures maximum effectiveness of this method.
SleepDuration = 1  ; This can sometimes be finely adjusted (e.g. 2 is different than 3) depending on the value below.
TimePeriod = 3 ; Try 7 or 3.  See comment below.
; On a PC whose sleep duration normally rounds up to 15.6 ms, try TimePeriod=7 to allow
; somewhat shorter sleeps, and try TimePeriod=3 or less to allow the shortest possible sleeps.

DllCall("Winmm\timeBeginPeriod", uint, TimePeriod)  ; Affects all applications, not just this script's DllCall("Sleep"...), but does not affect SetTimer.
Iterations = 83
StartTime := A_TickCount

Loop %Iterations%
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.

DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
MsgBox % "Sleep duration = " . (A_TickCount - StartTime) / Iterations
你可以试着改编一下。我已经开始:

SetBatchLines -1 ; This makes your script run at the fastest speed possible, thereby increasing the likelihood that you'll get exactly what time you want to get.
SleepDuration = 1 ; This and the line before it should go at the beginning of your script.

DllCall("Winmm\timeBeginPeriod", uint, TimePeriod) ; this must be before the actual sleep DllCall
Iterations = 83
StartTime := A_TickCount ; You can delete this once you know it works

Loop %Iterations%
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.

DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal, after the middle DllCall.
MsgBox % "Sleep duration = " . (A_TickCount - StartTime) / IterationsMsgBox % "Sleep duration = " . (A_TickCount - StartTime) / Iterations ; Also delete this once you know it works

您是如何测试准确的延迟的?您是否尝试过在中调整示例?