Browse Source

update holdrepeat lib to be more DRY

master
Mar Alegre 1 year ago
parent
commit
3bc32581a0
  1. 126
      lib/HoldRepeat.ahk
  2. 2
      lib/Prelude.ahk
  3. 22
      src/Deep Rock Galactic.ahk

126
lib/HoldRepeat.ahk

@ -1,97 +1,87 @@
;
; Functions for holding and repeating keys ; Functions for holding and repeating keys
;
; Internal Functions
; ;
_HoldRepeat_TurboOn := Map() _HoldRepeat_LoopState := Map()
_HoldRepeat_IsActive := Map()
_GetPrefix(SendBlind) { _HoldRepeat_Handler(Mode, TargetKey, CancelKeys:=[], HoldKey?, BlindMode:="{Blind}") {
if (SendBlind) { global _HoldRepeat_IsActive
return "{Blind}" 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
} else { } else {
return "" return
} }
SetTimer _HoldRepeat_Loop.Bind(TargetKey,CancelKeys,HoldKey?,BlindMode), 50
} }
Hold(Primary, Secondary:=[], Tertiary:=[], SendBlind:=true) { _HoldRepeat_Loop(TargetKey, CancelKeys, HoldKey?, BlindMode:="{Blind}") {
Prefix := _GetPrefix(SendBlind) global _HoldRepeat_LoopState
Send Prefix "{" Primary " down}" global _HoldRepeat_IsActive
SetTimer HoldLoop.Bind(Primary,Secondary,Tertiary,Prefix), 50
}
HoldLoop(Primary, Secondary, Tertiary, Prefix) { endLoop := false
if GetKeyState(Primary,"P") { for index,key in CancelKeys {
SetTimer , 0
return
}
for index,key in Secondary {
if GetKeyState(key,"P") { if GetKeyState(key,"P") {
Send Prefix "{" Primary " up}" Send BlindMode "{" TargetKey " up}"
SetTimer , 0 endLoop := true
return
} }
} }
keyHeld:=!Tertiary.Length if IsSet(HoldKey) {
for index,key in Tertiary { if !GetKeyState(HoldKey,"P") {
if GetKeyState(key) { Send BlindMode "{" TargetKey " up}"
keyHeld:=1 endLoop := true
break }
} else {
if GetKeyState(TargetKey,"P") {
endLoop := true
} }
} }
if (!keyHeld) { if endLoop {
Send Prefix "{" Primary " up}"
SetTimer , 0 SetTimer , 0
_HoldRepeat_IsActive[TargetKey] := false
return return
} }
}
Repeat(Primary, Secondary:=[], SendBlind:=true) { if _HoldRepeat_LoopState.Has(TargetKey) {
Prefix := _GetPrefix(SendBlind) updown := (_HoldRepeat_LoopState[TargetKey] ? "up" : "down")
Send Prefix "{" Primary " up}" Send BlindMode "{" TargetKey " " updown "}"
Sleep 100 _HoldRepeat_LoopState[TargetKey] := !_HoldRepeat_LoopState[TargetKey]
RepeatVar := 0
Loop {
if GetKeyState(Primary,"P") {
return
}
for index,key in Secondary {
if GetKeyState(key,"P") {
return
}
}
if (RepeatVar == 0) {
Send Prefix "{" Primary " down}"
RepeatVar := 1
} else {
Send Prefix "{" Primary " up}"
RepeatVar := 0
}
Sleep 50
} }
} }
;
; Internal Functions
;
Hold := _HoldRepeat_Handler.Bind("Hold")
Repeat := _HoldRepeat_Handler.Bind("Repeat")
; 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(Primary, Secondary:=[], SendBlind:=True) { HoldOrRepeat(TargetKey, CancelKeys?, BlindMode:="{Blind}") {
Send _GetPrefix(SendBlind) "{" Primary " down}" Send BlindMode "{" TargetKey " down}"
if KeyWait(Primary, "D T0.2") { if KeyWait(TargetKey, "D T0.2") {
Repeat(Primary,Secondary,SendBlind) mode := "Repeat"
} else { } else {
Hold(Primary,Secondary,,SendBlind) mode := "Hold"
} }
_HoldRepeat_Handler(mode, TargetKey, CancelKeys,, BlindMode)
} }
; Press Target button extremely rapidly while Hold button is held down ; Press Target button extremely rapidly while Hold button is held down
Turbo(Target, Hold:=Target) { Turbo(TargetKey, HoldKey:=TargetKey,BlindMode:="{Blind}") {
global _HoldRepeat_TurboOn While GetKeyState(HoldKey,"P") {
If (_HoldRepeat_TurboOn.Has(Target "_" Hold) and _HoldRepeat_TurboOn[Target "_" Hold]) { Send BlindMode "{" TargetKey "}"
return
} else {
_HoldRepeat_TurboOn[Target "_" Hold]:=1
While GetKeyState(Hold,"P") {
SendEvent "{Blind}{" Target " down}"
Sleep 10
SendEvent "{Blind}{" Target " up}"
Sleep 10
}
_HoldRepeat_TurboOn[Target "_" Hold]:=0
} }
} }

2
lib/Prelude.ahk

@ -23,5 +23,5 @@ A_MenuMaskKey := "vkFF"
#SingleInstance force #SingleInstance force
; Send Mode ; Send Mode
SendMode "Input" SendMode "Event"
SetKeyDelay -1 SetKeyDelay -1

22
src/Deep Rock Galactic.ahk

@ -19,10 +19,10 @@ ToggleFullAuto(Index) {
global FullAutoEnabled global FullAutoEnabled
FullAutoEnabled[Index] := !FullAutoEnabled[Index] FullAutoEnabled[Index] := !FullAutoEnabled[Index]
EnabledDisabled := (FullAutoEnabled[Index] ? "enabled" : "disabled") EnabledDisabled := (FullAutoEnabled[Index] ? "enabled" : "disabled")
if (Index < 5) { if (Index == 5) {
ToolTip "Full Auto " EnabledDisabled " for " Index, A_ScreenWidth // 2, A_ScreenHeight // 2 ToolTip "Auto jump " EnabledDisabled, A_ScreenWidth // 2, A_ScreenHeight // 2
} else { } else {
ToolTip "Extra macros " EnabledDisabled, A_ScreenWidth // 2, A_ScreenHeight // 2 ToolTip "Full Auto " EnabledDisabled " for " Index, A_ScreenWidth // 2, A_ScreenHeight // 2
} }
SetTimer RemoveTooltip, -1000 SetTimer RemoveTooltip, -1000
} }
@ -54,28 +54,26 @@ RemoveTooltip() {
; Do full auto when caps lock is on for enabled weapons ; Do full auto when caps lock is on for enabled weapons
#HotIf (GetKeyState("CapsLock","T") and FullAutoEnabled[EquipState]) #HotIf (GetKeyState("CapsLock","T") and FullAutoEnabled[EquipState])
*LButton::Turbo("LButton") *LButton::Repeat("LButton",,"LButton")
; Hold space rapidly jumps
#HotIf FullAutoEnabled[5]
*Space::Turbo("Space")
; ;
; Other ; Other
; ;
#HotIf FullAutoEnabled[5] #HotIf
; Alt + E holds E ; Alt + E holds E
*!e UP::Hold("e",["w","a","s","d","Space","LButton","RButton"]) *!e UP::Hold("e",["w","a","s","d","Space","LButton","RButton"])
; Alt + W holds W
*!w UP::Hold("w",["s"])
; Press shift to sprint, only toggles on ; Press shift to sprint, only toggles on
*LShift::{ *LShift::{
Send "{Blind}{LShift up}{LShift down}" Send "{Blind}{LShift up}{LShift down}"
KeyWait("LShift") KeyWait("LShift")
} }
*LShift UP::Hold("LShift",,["w"]) *LShift UP::Hold("LShift",,"w")
; Hold space rapidly jumps
*Space::Turbo("Space")
; Grenade animation canceling ; Grenade animation canceling
*MButton::Send "{Blind}{LCtrl down}{MButton down}" *MButton::Send "{Blind}{LCtrl down}{MButton down}"

Loading…
Cancel
Save