Browse Source

update & simplify HoldRepeat library

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

84
lib/HoldRepeat.ahk

@ -1,87 +1,87 @@
; Functions for holding and repeating keys ; Functions for holding and repeating keys
; ;
; Private Functions ; Private Methods
; ;
_HoldRepeat_LoopState := Map()
_HoldRepeat_IsActive := Map() _HoldRepeat_IsActive := Map()
_HoldRepeat_Handler(Mode, TargetKey, CancelKeys:=[], HoldKey?, BlindMode:="{Blind}") { _HoldRepeat_Handler(IsHold, TargetKey, CancelKeys:=[], HoldKey:="") {
global _HoldRepeat_IsActive global _HoldRepeat_IsActive
global HoldRepeat_BlindMode
global HoldRepeat_TimerPeriod
if (_HoldRepeat_IsActive.Has(TargetKey) AND _HoldRepeat_IsActive[TargetKey]) { if (_HoldRepeat_IsActive.Has(TargetKey) AND _HoldRepeat_IsActive[TargetKey]) {
return return
} }
_HoldRepeat_IsActive[TargetKey] := true _HoldRepeat_IsActive[TargetKey] := true
Send BlindMode "{" TargetKey " down}" if (CancelKeys.Length == 0 AND !HoldKey) {
if (Mode == "Hold") { if IsHold {
if _HoldRepeat_LoopState.Has(TargetKey) { CancelKeys:=[TargetKey]
_HoldRepeat_LoopState.Delete(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_LoopState
global _HoldRepeat_IsActive global _HoldRepeat_IsActive
global HoldRepeat_BlindMode
endLoop := false endLoop := false
for index,key in CancelKeys { for index,key in CancelKeys {
if GetKeyState(key,"P") { if GetKeyState(key,"P") {
Send BlindMode "{" TargetKey " up}"
endLoop := true endLoop := true
break
} }
} }
if IsSet(HoldKey) { if (HoldKey AND !GetKeyState(HoldKey,"P")) {
if !GetKeyState(HoldKey,"P") { endLoop := true
Send BlindMode "{" TargetKey " up}"
endLoop := true
}
} else {
if GetKeyState(TargetKey,"P") {
endLoop := true
}
} }
if endLoop { if endLoop {
SetTimer , 0 SetTimer , 0
if (IsHold AND !GetKeyState(TargetKey,"P")) {
Send HoldRepeat_BlindMode "{" TargetKey " up}"
}
_HoldRepeat_IsActive[TargetKey] := false _HoldRepeat_IsActive[TargetKey] := false
return return
} }
if _HoldRepeat_LoopState.Has(TargetKey) { if !IsHold {
updown := (_HoldRepeat_LoopState[TargetKey] ? "up" : "down") Send HoldRepeat_BlindMode "{" TargetKey "}"
Send BlindMode "{" TargetKey " " updown "}"
_HoldRepeat_LoopState[TargetKey] := !_HoldRepeat_LoopState[TargetKey]
} }
} }
; ;
; Public Functions ; Public Methods
; ;
Hold := _HoldRepeat_Handler.Bind("Hold") ; Make BlindMode a public variable so it doesn't have to be passed as an input to every function
Repeat := _HoldRepeat_Handler.Bind("Repeat") ; 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 ; Sets key to hold if single tap, or repeat if double tap
HoldOrRepeat(TargetKey, CancelKeys?, BlindMode:="{Blind}") { HoldOrRepeat(TargetKey, CancelKeys?, HoldKey?) {
Send BlindMode "{" TargetKey " down}" global HoldRepeat_BlindMode
Send HoldRepeat_BlindMode "{" TargetKey " down}"
if KeyWait(TargetKey, "D T0.2") { if KeyWait(TargetKey, "D T0.2") {
mode := "Repeat" isHold := false
} else { } else {
mode := "Hold" isHold := true
}
_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 "}"
} }
_HoldRepeat_Handler(isHold, TargetKey, CancelKeys, HoldKey)
} }
Loading…
Cancel
Save