One way to get around a window that has lost focus is to send a false or dummy keypress first. Say you send the key "H" for whatever command. In the edit screen instead of using Active Window, select your application instead. Now, if you sent the H and that app wasn't in focus, it might take some time to do that, and the keypress might slip past. So, in all your commands that send info to the app window, first send a key that doesn't do anything. I use the Left Shift by itself since I don't use that for other commands. Doesn't even have to be long, I use just 0.01 secs. But that will pop the app back into focus in time for the real command to hit.
Not saying that is the solution here, but if lost focus IS the problem, it will help.
I also use this same trick so I can look at other stuff like a browser while the game is playing, and if I give a command or touch the joystick, it triggers refocus so I can jump back into action instead of wondering why nothing is suddenly working as I dive into a star.