3

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?

Community
  • 1
  • 1
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

2 Answers2

5

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • If I'm going to leave the Python script idea and embracing a tool like this, wouldn't be better to use some macro creator with a GUI? xdotool looks too simple – Roman Rdgz Jan 23 '13 at 09:07
  • 1
    Use Python to build a wrapper for the script. This way, you can create yourself a macro generator and executor. – Aaron Digulla Jan 23 '13 at 10:45
2

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')