When I was experimenting with python a while ago, I wanted to move the mouse pointer from inside python. Since this didn’t seem to be possible, I found a workaround which used a c-call to X11/Xlib to actually move the mouse pointer. This really worked great and I will use this as an example to show how to call a c-function from within python.
First, here’s the c-file containing a method to move the mouse pointer:
void mouseMove(int x, int y)
Display *displayMain = XOpenDisplay(NULL);
if(displayMain == NULL)
fprintf(stderr, “no Display !!!\n”);
XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);
when compiled, this gives us a share-library called mouse.so.
Now, on the python side, we have to import some c-specific stuff first:
from ctypes import cdll,c_int
and can then load the library as follows:
lib = “/path/to/mouse.so”
dll = cdll.LoadLibrary(lib)
and finally map the function call like this:
mouseMove = (lambda x,y: dll.mouseMove(c_int(x), c_int(y)))
now we can move our mouse-pointer from within python by calling mouseMove()