I want to simulate mouse clicks in Python (and if it is possible, also keyboard inputs) in a Linux system (Ubuntu), so I am able to create a macro for an external program.
I have researched a bit, and found questions like this which use win32api or ctypes to do the job. But since I am working in Ubuntu, those doesn't fit for me.
I also found Xaut (former Xautomation), which is said to simulate mouse and keyboard interactions over Linux systems. But it is hard to install for me, and it lacks documentation and examples anyway.
I really like the win32api solution, it's very simple. Isn't there any similar solution for Linux?
2 Answers 2
Unlike Windows, Linux is a secure system by design so injecting/intercepting user events isn't simple :-)
But there are tools which can do the same on Linux (at least when they have xauth credentials, so no snooping/cracking other users on the same computer). Try xdotool which offers a wide range of commands to find and select windows and then send events to them.
xdotool is available for most Linux distributions (debian and rpm based).
Note: For security reasons, synthetic events in X11 have a flag set and some software ignores all events with this flag.
2 Comments
You can use this:
import pyautogui
x= #x position
y= #y position
pyautogui.click(x,y)
To install pyautogui visit the official installation page documentation: https://pyautogui.readthedocs.io/en/latest/install.html
To write text automatically use this:
import pyautogui
pyautogui.write('write this example')