diff --git a/bongo_cat_presser.ahk b/bongo_cat_presser.ahk index e6cbdf3..a725930 100644 --- a/bongo_cat_presser.ahk +++ b/bongo_cat_presser.ahk @@ -11,17 +11,51 @@ #SingleInstance, Force #Persistent ; Keeps the script running to allow for the high-frequency timer. +#InstallKeybdHook ; Hook-based detection for instant modifier response. +#InstallMouseHook ; Hook-based detection for instant mouse button response. SetBatchLines, -1 +; Force 1ms timer resolution (default is ~15.6ms, making SetTimer and Sleep inaccurate). +DllCall("winmm\timeBeginPeriod", "UInt", 1) +OnExit("CleanupTimerResolution") + ; --- CONFIGURATION --- KEYS_TO_PRESS := ["F21", "F22", "F23", "F24"] ; A pool of safe keys to press at random. PRESS_INTERVAL_MS := 10 ; How often to press a key (in milliseconds). CHECK_INTERVAL_MS := 5 ; How often to check for user input (for pausing). -; --- MAIN SCRIPT LOGIC --- +; --- STATE --- global Toggle := false +global ModifierHeld := false global lastPressTime := 0 +; --- MODIFIER HOOKS --- +; ~ lets the key pass through normally, * fires regardless of other modifiers. +; These fire instantly via the keyboard hook — no polling delay. +~*LShift:: +~*RShift:: +~*LCtrl:: +~*RCtrl:: +~*LAlt:: +~*RAlt:: +~*LButton:: +~*RButton:: + ModifierHeld := true +return + +~*LShift Up:: +~*RShift Up:: +~*LCtrl Up:: +~*RCtrl Up:: +~*LAlt Up:: +~*RAlt Up:: +~*LButton Up:: +~*RButton Up:: + ; Only clear if no modifiers are still physically held. + if !GetKeyState("Shift", "P") and !GetKeyState("Control", "P") and !GetKeyState("Alt", "P") and !GetKeyState("LButton", "P") and !GetKeyState("RButton", "P") + ModifierHeld := false +return + F8:: Toggle := !Toggle if (Toggle) { @@ -33,9 +67,8 @@ F8:: return MainLoop: - ; THE FIX: This loop runs very fast (every 10ms) to check for user input. - if GetKeyState("Shift", "P") or GetKeyState("Control", "P") or GetKeyState("Alt", "P") or GetKeyState("LButton", "P") or GetKeyState("RButton", "P") - return ; Instantly skip if a modifier is held. + if (ModifierHeld) + return ; Now, check if enough time has passed to send the NEXT keypress. if (A_TickCount - lastPressTime > PRESS_INTERVAL_MS) @@ -53,6 +86,10 @@ MainLoop: } return +CleanupTimerResolution() { + DllCall("winmm\timeEndPeriod", "UInt", 1) +} + Esc:: ExitApp return \ No newline at end of file