Awesome Quest wrote: ↑19 Jan 2022, 06:41
I think this has to do with how setTimer handle's multiple timers, and since the two functions don't have the same memory the new timer doesn't cull the old timer.
That is correct. To reset a previously set timer, you must pass SetTimer the
same function. In your case, that means using one function for both timers.
This might be a stupid question, but can you get two anonymous functions to share memory without declaring the function?
You don't. If they occupied the same exact location in memory, it would literally be one function.
Your original definition creates a global constant "altUpFun" which contains a reference to a function. Since Send has no return value (and the return value doesn't matter), it is equivalent to write
altUpFun() => Send("{Alt Up}"). If you wrote
altUpFun := () => Send("{Alt Up}"), it would almost be the same, except that
altUpFun is truly
variable (you can assign to it) and the function is anonymous (
altUpFun.Name is "").
altUpFun := Send.Bind("{Alt Up}") would give a similar result. While technically you get an anonymous function that you can pass to both SetTimer calls, there isn't any point in it being anonymous.
Of course, you can also pass
Send.Bind("{Alt up}") directly to SetTimer, with the same problem as before because each call will create a new timer.
If you want to "make this less spaghetti", consider the overall pattern and the parts you are repeating:
- Define a hotkey.
- Send Alt down.
- Send some keys that vary.
- Set a timer to release Alt.
Then make a function that does that. The end result could look like:
Code: Select all
AltTabHotkey "~F13", "{Tab}"
AltTabHotkey "~F14", "{Shift down}{Tab}{Shift up}"
or
Code: Select all
~F13::SendAlt "{Tab}"
~F14::SendAlt "{Shift down}{Tab}{Shift up}"
With the rest done by the function (I'll leave that part for you to figure out).
Alternatively, instead of defining a function that just releases Alt, you can define a function that
sets the timer:
Code: Select all
SendAltUpAfter1s() => SetTimer(() => Send("{Alt up}"), -1000)
There is only one anonymous function, so the timer is reset each time the function is called.
Depending on how you're producing F13/F14 events, you might want to add
* so that the hotkeys can be used even while Alt is down due to Send. Unlike
*~F13,
~F13:: won't activate if Alt is down (but if F13 is produced by software, that software could temporarily release Alt).