Macbook OpenEmu Python send keystrokes
<p>I am really impressed by <a href="https://github.com/pakoito/MarI-O" rel="nofollow">this</a> MarlIO project and want to implement something similar using Python. However, I got the emulator <a href="http://openemu.org/" rel="nofollow">OpenEmu</a> working, however, I don't know how to control the game using Python. </p>

<p>Isn't it just a matter of sending a few keystrokes?! Man, it is not that straightforward on a Mac. </p>

<pre><code>In [41]: cmd1
Out[41]: '\nosascript -e \'tell application "System Events" to key code 48 using {command down}\' \n'

In [42]: cmd2
Out[42]: '\nosascript -e \'tell application "System Events" to keystroke "a"\' \n'
</code></pre>

<p>I want to first use "COMMAND+TAB" to switch to the openEmu and then hit "a" to jump. However, when I ran the two commands, it only switched to the OpenEmu, looks like the keystroke 'a' did not got sent. </p>

<p><a href="http://i.stack.imgur.com/pblXX.png" rel="nofollow"><img src="http://i.stack.imgur.com/pblXX.png" alt="enter image description here"></a></p>

<p>However, when I ran 'cmd2' separately, it was clearly working. 
Then I testedit against a different application - sublime, and it seemed to work there. </p>

<p><a href="http://i.stack.imgur.com/bqmqo.png" rel="nofollow"><img src="http://i.stack.imgur.com/bqmqo.png" alt="enter image description here"></a></p>

<p>Can anyone point me to the right direction what I really did wrong with OpenEmu? </p>

<p>I did something like that a few months ago. The keystrokes are sent. However, System Event keystrokes last virtually no time, so the emulator's input mechanism doesn't pick them up.</p>

<p>I couldn't find a way to ask for a duration with AppleScript, so I ended up solving the problem using <a href="https://developer.apple.com/reference/coregraphics/1658572-quartz_event_services" rel="nofollow">Quartz event taps</a>, which let you do, for instance, "start pressing key, sleep 0.1s, stop pressing key". I did it in Swift, but you should be able to do it in Python with the <code>ctype</code> module.</p>

<p>Also note that it might be difficult to synchronize on a frame basis with the emulator. I <a href="https://github.com/OpenEmu/OpenEmu/pull/2378" rel="nofollow">raised that problem with the project maintainers</a>, but I turned away because of the relatively cold response.</p>

python osx emulator osascript