Browse Source

update & simplify HoldRepeat library

master
Mar Alegre 1 year ago
parent
commit
9325e1f0e6
  1. 82
      lib/HoldRepeat.ahk

82
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)
}
} else if (Mode == "Repeat") {
_HoldRepeat_LoopState[TargetKey] := true
if (CancelKeys.Length == 0 AND !HoldKey) {
if IsHold {
CancelKeys:=[TargetKey]
} else {
return
HoldKey:=TargetKey
}
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") {
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)
}
Loading…
Cancel
Save