How to simulate mouse clicks using Xlib in Python

For educational purposes, I decided to write a python script with cwiidand Xlibso that I can use my wiimote as a mouse.

So far I have a cursor to move, calling disp.warp_pointer(dx,dy), and then calling disp.sync()every given time interval. I am afraid that this may not be the most efficient way to do this, but at least for now it is simple and works quite well.

The problem I am facing is mouse clicks. How to simulate a mouse click in Xlib? I would like to separate events for the press and release so that I can drag things. I came across this , but all the solutions there seem to use other libraries.

+3
source share
2 answers

In plain Xlib (C language) you can use XTestExtensionor XSendEvent(). I'm not sure about their bindings to python. There are probably bindings for their xcb versions using xpyb.

There is also binary code xtefrom the package xautomation(on Debian sudo apt-get install xautomation, and then man xte). xtevery easy to use, and you can also look at its source code to find out how to use it XTestExtension.

Pointers

+3
source

using only python Xlib:

from Xlib import X
from Xlib.display import Display
from Xlib.ext.xtest import fake_input
d = Display()
fake_input(d, X.ButtonPress, 1)
d.sync()
fake_input(d, X.ButtonRelease, 1)
d.sync()

fake_input . 1/2/3 - // , 4/5 6/7 .

+2

All Articles