kopo222
kopo222

Reputation: 127

How to simulate a key press in Python on a linux machine

How do you simulate a key press in python on a linux machine?

This is for use with an emulator and making a bot which can play a game.

So primarily the 'wasd' keys, space and so on, this thread here is more or less what I want, however I believe that this solution is windows specific using

ctypes.windll

I believe that the main problem and why this is hard to do is to do with 'ScanCodes' and 'VKs', games tend to ignore as this is not how the user interacts with the game

So is there any linux workaround like the above for linux? Any help is appreciated, thank you.

Upvotes: 3

Views: 1803

Answers (1)

Simon
Simon

Reputation: 5698

I had the same problem when using pyautogui. I just seemed to not have the right focus. Selecting the window in a few different ways did not help. Using xdo however, I managed to get the desired result.

Example:

from xdo import Xdo

xdo = Xdo()
win_id = xdo.get_active_window()
print(xdo.get_window_name(win_id))
xdo.send_keysequence_window(win_id, "Return")

More information: https://github.com/rshk/python-libxdo

Upvotes: 4

Related Questions