Macbook OpenEmu Python send keystrokes

懵懂的女人 提交于 2021-02-10 07:53:58

问题


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.

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

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


回答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.



来源:https://stackoverflow.com/questions/39671998/macbook-openemu-python-send-keystrokes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!