Essentially, if I start a loop and have it simply "Type a keystroke" of the letter "a" over and over, I can watch KM's memory climb in activity monitor until the entire system hangs.
However, if I do the same thing and run Applescript with:
tell application "System Events" to keystroke "a"
I don't see the same issue, but at some point will likely get an abort with:
Action 63319 failed: Execute an AppleScript failed with script error: text-script:34:45: execution error: System Events got an error: Connection is invalid. (-609)
So I can use the KM "Type a Keystroke" and it will work great until the system runs out of memory or use the Applescript and be good until the OS barfs.
Any suggestion on a solution? Ideally one that uses the built in keystroke command without the memory issues?
The use case is almost exactly what I presented above. I have a game where I use a cmd+shift+letter command to toggle on the repeated press of a key (or set of keys) until I turn them off.
For my real life use case I actually add a bit of a delay, which prevents the memory creep from happening as fast, but it still happens.
If I do this instead, it will not memory leak but will eventually error out with the error:
Action 63319 failed: Execute an AppleScript failed with script error: text-script:34:45: execution error: System Events got an error: Connection is invalid. (-609)
That, in a round-about way, was the point of the question -- how much of a delay can you stand? The extra info would be "and how many keypresses is your maximum need?".
I'm wondering if you're submitting events faster than they can be processed by the app, leading to a buildup of either queued or "not completed" (where the app silently drops them) events, hence the bloat. I don't know much (anything!) about how KM does things "behind the scenes" but you may be in for some process management, maybe increasing the delay as required to control memory usage.
Functionally, it works exactly as expected. However, I ran it for 60 seconds, typing a single letter over and over into sublime text editor without the delay (and no other macros in between) and this is what I see:
I was playing with this yesterday and, for me at least, I could run your original macro for a couple of minutes, end it by pressing the ⌘ key, and characters continued to be typed into Script Editor for ages. That suggests to me that KM is buffering simulated keypresses and managing their submission to the event queue -- the memory bloat is possibly the buffering.