2

I am really impressed by this MarlIO project and want to implement something similar using Python. However, I got the emulator OpenEmu working, however, I don't know how to control the game using Python.

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

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'

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.

enter image description here

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

enter image description here

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

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178
  • Did you success plug `Python AI` into OSX OpenEmu? If so please shed me the light – joe Feb 02 '20 at 06:51

1 Answers1

1

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.

I couldn't find a way to ask for a duration with AppleScript, so I ended up solving the problem using Quartz event taps, 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 ctype module.

Also note that it might be difficult to synchronize on a frame basis with the emulator. I raised that problem with the project maintainers, but I turned away because of the relatively cold response.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • Is your custom build working well with AppleScript? Could you post a working snippet? – Pwdr Jul 16 '17 at 17:42
  • @Pwdr, I stopped short of implementing input support. If you want an event tap example, I can probably come up with one. – zneak Jul 16 '17 at 18:06
  • That would be awesome! – Pwdr Jul 16 '17 at 18:08
  • 1
    @Pwdr, I don't have the code anymore, but [this guy](https://stackoverflow.com/a/3926582/251153) has a minimal sample where he sends a key down and then a key up. You need to insert a delay/sleep between the key down and the key up that will ensure that the emulator's run loop notices that the key is down. Depending on the game itself too, you'd be looking at 20-300ms; YMMV. Notice that the keystrokes always apply to the foreground app, so you need to start your script and then give yourself some time to set the focus back to OpenEmu. – zneak Jul 16 '17 at 18:15