There is no other way to do this in a loop without making constant checks like you are doing. It could maybe be done a little cleaner, but you would still need to use the same basic technique.
If you want to shorten the code, what you could maybe do is put all the timings into an array, and then parse the array.
Something like this:
Code: Select all
#SingleInstance force
actions := [{key: "{6}", delay: -1}
, {keys: "{LButton}", delay: 475}
, {keys: "-1", delay: 475}
, {keys: "-1", delay: 475}
, {keys: "{LButton up}", delay: 8}]
currentPos := 1
max := actions.Length()
timerOn := 0
#::
if (timerOn)
return ; Filter key repeats
timerOn := 1
GoSub, SendKeys
return
# up::
timerOn := 0
SetTimer, SendKeys, Off
currentPos := 1 ; If you want to reset position for each press, keep this line, else delete it
return
SendKeys:
Loop {
;~ OutputDebug % "AHK| Processing item #" currentPos
obj := actions[currentPos]
; Increment position counter, and handle wrapping back around to start
currentPos++
if (currentPos > max)
currentPos := 1
; Process the item
if (obj.Keys != -1)
Send % obj.Keys
;~ OutputDebug % "AHK| Sending " obj.Keys
if (obj.Delay == -1){
; Key has a delay of -1 (Meaning "No Delay"), so just go straight on to the next item
;~ OutputDebug % "AHK| No delay after item"
continue
} else {
; Key has a delay after it, so start a ONE TIME timer for the delay until the next key
SetTimer, SendKeys, % "-" obj.Delay
;~ OutputDebug % "AHK| Delaying " obj.Delay
; Now break out of the loop
break
}
}
;~ OutputDebug % "AHK| Sending complete"
return
So in order to use it, you need to be able to build the array which holds the data.
This is controlled by this code:
Code: Select all
actions := [{key: "{6}", delay: -1}
, {keys: "{LButton}", delay: 475}
, {keys: "-1", delay: 475}
, {keys: "-1", delay: 475}
, {keys: "{LButton up}", delay: 8}]
The structure of actions:
Start with an empty indexed array:
actions := []
Then, each item in the array describes what to do for each "loop".
It has two elements:
"Key" describes what to send. A value of -1 means "Send no key"
"Delay" describes how long to wait afterwards. A value of -1 means "No delay after this key"
eg this means "Send the 6 key but do not wait after"
{key: 6, delay: -1}
and this means "Send LButton and wait 475ms"
{keys: "{LButton}", delay: 475}
and this means "Send no key, and wait 475ms"
{keys: "-1", delay: 475}