From 9325e1f0e6d732c1d2cef87c4f502f6a820336e6 Mon Sep 17 00:00:00 2001
From: mar <mar@mar.alemor.org>
Date: Mon, 17 Jun 2024 15:52:30 -0400
Subject: [PATCH] update & simplify HoldRepeat library

---
 lib/HoldRepeat.ahk | 84 +++++++++++++++++++++++-----------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/lib/HoldRepeat.ahk b/lib/HoldRepeat.ahk
index 2c0159a..b25e583 100644
--- a/lib/HoldRepeat.ahk
+++ b/lib/HoldRepeat.ahk
@@ -1,87 +1,87 @@
 ; Functions for holding and repeating keys
 
 ;
-; Private Functions
+; Private Methods
 ;
 
-_HoldRepeat_LoopState := Map()
 _HoldRepeat_IsActive := Map()
 
-_HoldRepeat_Handler(Mode, TargetKey, CancelKeys:=[], HoldKey?, BlindMode:="{Blind}") {
+_HoldRepeat_Handler(IsHold, TargetKey, CancelKeys:=[], HoldKey:="") {
     global _HoldRepeat_IsActive
+    global HoldRepeat_BlindMode
+    global HoldRepeat_TimerPeriod
+
     if (_HoldRepeat_IsActive.Has(TargetKey) AND _HoldRepeat_IsActive[TargetKey]) {
         return
     }
     _HoldRepeat_IsActive[TargetKey] := true
 
-    Send BlindMode "{" TargetKey " down}"
-    if (Mode == "Hold") {
-        if _HoldRepeat_LoopState.Has(TargetKey) {
-            _HoldRepeat_LoopState.Delete(TargetKey)
+    if (CancelKeys.Length == 0 AND !HoldKey) {
+        if IsHold {
+            CancelKeys:=[TargetKey]
+        } else {
+            HoldKey:=TargetKey
         }
-    } else if (Mode == "Repeat") {
-        _HoldRepeat_LoopState[TargetKey] := true
-    } else {
-        return
     }
-    SetTimer _HoldRepeat_Loop.Bind(TargetKey,CancelKeys,HoldKey?,BlindMode), 50
+
+    if IsHold {
+        Send HoldRepeat_BlindMode "{" TargetKey " down}"
+    }
+
+    SetTimer _HoldRepeat_Loop.Bind(IsHold, TargetKey, CancelKeys, HoldKey), HoldRepeat_TimerPeriod
 }
 
-_HoldRepeat_Loop(TargetKey, CancelKeys, HoldKey?, BlindMode:="{Blind}") {
+_HoldRepeat_Loop(IsHold, TargetKey, CancelKeys, HoldKey) {
     global _HoldRepeat_LoopState
     global _HoldRepeat_IsActive
+    global HoldRepeat_BlindMode
 
     endLoop := false
     for index,key in CancelKeys {
         if GetKeyState(key,"P") {
-            Send BlindMode "{" TargetKey " up}"
             endLoop := true
+            break
         }
     }
-    if IsSet(HoldKey) {
-        if !GetKeyState(HoldKey,"P") {
-            Send BlindMode "{" TargetKey " up}"
-            endLoop := true
-        }
-    } else {
-        if GetKeyState(TargetKey,"P") {
-            endLoop := true
-        }
+    if (HoldKey AND !GetKeyState(HoldKey,"P")) {
+        endLoop := true
     }
+
     if endLoop {
         SetTimer , 0
+        if (IsHold AND !GetKeyState(TargetKey,"P")) {
+            Send HoldRepeat_BlindMode "{" TargetKey " up}"
+        }
         _HoldRepeat_IsActive[TargetKey] := false
         return
     }
 
-    if _HoldRepeat_LoopState.Has(TargetKey) {
-        updown := (_HoldRepeat_LoopState[TargetKey] ? "up" : "down")
-        Send BlindMode "{" TargetKey " " updown "}"
-        _HoldRepeat_LoopState[TargetKey] := !_HoldRepeat_LoopState[TargetKey]
+    if !IsHold {
+        Send HoldRepeat_BlindMode "{" TargetKey "}"
     }
 }
 
 ;
-; Public Functions
+; Public Methods
 ;
 
-Hold := _HoldRepeat_Handler.Bind("Hold")
-Repeat := _HoldRepeat_Handler.Bind("Repeat")
+; Make BlindMode a public variable so it doesn't have to be passed as an input to every function
+; plus it's highly unlikely we will want different modes in different hotkeys for the same script
+HoldRepeat_BlindMode := "{Blind}"
+HoldRepeat_TimerPeriod := 100
+
+Hold := _HoldRepeat_Handler.Bind(true)
+Repeat := _HoldRepeat_Handler.Bind(false)
 
 ; Sets key to hold if single tap, or repeat if double tap
-HoldOrRepeat(TargetKey, CancelKeys?, BlindMode:="{Blind}") {
-    Send BlindMode "{" TargetKey " down}"
+HoldOrRepeat(TargetKey, CancelKeys?, HoldKey?) {
+    global HoldRepeat_BlindMode
+
+    Send HoldRepeat_BlindMode "{" TargetKey " down}"
     if KeyWait(TargetKey, "D T0.2") {
-        mode := "Repeat"
+        isHold := false
     } else {
-        mode := "Hold"
-    }
-    _HoldRepeat_Handler(mode, TargetKey, CancelKeys,, BlindMode)
-}
-
-; Press Target button extremely rapidly while Hold button is held down
-Turbo(TargetKey, HoldKey:=TargetKey,BlindMode:="{Blind}") {
-    While GetKeyState(HoldKey,"P") {
-        Send BlindMode "{" TargetKey "}"
+        isHold := true
     }
+    _HoldRepeat_Handler(isHold, TargetKey, CancelKeys, HoldKey)
 }
\ No newline at end of file