Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate mouse clicks with Xlib on Python

For educational purposes I've set out to write a python script using cwiid and Xlib so that I can use my wiimote like a mouse.

So far I've gotten the cursor to move by calling disp.warp_pointer(dx,dy) then calling disp.sync() every set time interval. I'm afraid that it might not be the most efficient way to do it but at least for now, it's simple and works well enough.

The problem I'm having more difficulty with is mouse clicks. How do I simulate a mouse click in Xlib? I would like separate press and release events so that I can drag and drop stuff. I've come across this post, but all of the solutions there seem to use other libraries.

like image 238
math4tots Avatar asked Feb 21 '23 18:02

math4tots


1 Answers

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

The third parameter to fake_input selects the mouse button being simulated. 1/2/3 being left/middle/right buttons, 4/5 and 6/7 should do vertical and horizontal wheel scrolls.

like image 76
user336851 Avatar answered Mar 03 '23 10:03

user336851