Hello, I have a script that presses certain numpad keys, but with the idea of learning and trying to make the remapping code as minimalistic as possible, I tried to make it slightly more Object oriented.
I use a timer class which manages the periodicity of keypresses, and then store each of their instances into an array for future use, but the problem is that Im unable to stop them after. Let me explain with some of the stripped code:
When I press F2, it calls a function that sends the keys
; #Warn ; Enable warnings to assist with detecting common errors.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
global timerArray
F2::
beginMacro("test_macro")
return
test_macro(){
sendKey("1", 500)
}
sendKey(varKey, period)
{
global timerArray
numpadKey := "Numpad" . varKey
send, {%numpadKey%}
if (period > 0){
timerArray.Push(new Timer(period, "sendNumpadKey", varKey))
}
}
sendNumpadKey(number){
sendKey(number, 0)
}
You may have noticed the variable timerArray. Thats where Im storing the instances of the arbitrary number of Timers I may create, so I can reference them later and turn them off when needed.
stopAllTimers()
{
global timerArray
for index, element in timerArray{
element.Delete()
}
}
But even though timerArray.Length would show the correct number of Timers in the array, and the Delete function is being called, the timers never stop. Feels as if the references stored in the array are not the original objects but a copy, so the originals timers are still alive and kicking.
But even though timerArray.Length would show the correct number of Timers in the array, and the Delete function is being called, the timers never stop.
Unfortunately already ditched this approach, wiped my brain, and moved on. I though this thread would die, long forgotten.
Later on I found I had a problem with the way I was handling variables, because I was using this script within a collection of multiple scripts, and noticed too late my "header" script was wrong and was only putting in the autoexec zone some of
the global variables, which created a whole lot of strange behaviour all around with global variables not getting set. Most probably why I could retrieve the length of "timerArray" even though, as swagfag said, it should have reinitialized the array every time.
I ll mark swagfag answer as accepted as it is indeed a huge bug that would stop it from working. Thanks for checking it out!