5

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.

Community
  • 1
  • 1
math4tots
  • 8,540
  • 14
  • 58
  • 95
  • The [python-uinput suggestion](http://stackoverflow.com/a/3572488/709852) on the question you linked to should work. As I understand it, that's actually injecting events in at the kernel level, so it doesn't matter whether you're using XLib or Tkinter or GTK or any other toolkit. – Henry Gomersall Apr 21 '12 at 08:33

2 Answers2

7

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.

user336851
  • 161
  • 2
  • 6
3

On plain Xlib (C language), you can use the XTestExtension or XSendEvent(). I'm not sure about their python bindings. There are probably bindings for their xcb versions using xpyb.

There's also a binary called xte from the xautomation package (on Debian, sudo apt-get install xautomation and then man xte). xte is very easy to use, and you can also look at its source code to learn how to use the XTestExtension.

Pointers:

pzanoni
  • 2,979
  • 2
  • 18
  • 18
  • 2
    Upon further searching, it looks like `Xlib.ext.xtest` on Python may be equivalent to `XTestExtensions`. At any rate `xtest.fake_input` works great. – math4tots Apr 21 '12 at 18:50
  • @math4tots: Thanks for your comment, I was trying to accomplish the same but I couldn't find how to call XTest* methods. Regards, – Sebastian Nov 29 '12 at 14:53