Results 1 to 10 of 10

Thread: Controlling mouse with Python in X

  1. #1
    Join Date
    Jul 2008
    Beans
    15

    Question Controlling mouse with Python in X

    Hi there,
    I need to move the mouse cursor and generate mouse click events in X, and I'm hoping to use Python.

    I understand that the standard Xlib library provides a XWarpPointer function which moves the cursor around. This works for me in C, but I can't seem to find the equivalent function in Python-xlib. Any suggestions would be appreciated

    Also, how would I go about creating a click event once I have the cursor in the right position?

    Regards,
    Nemtaro

  2. #2
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Controlling mouse with Python in X

    at a glance, http://x-python.sourceforge.net/Doc/x/index.html does not mention XWarp (I know nothing about X windows, sorry). Did you tried to ask at x-python devel list?

    Another option will be to create custom wrapper for C functions you need.

  3. #3
    Join Date
    Jul 2008
    Beans
    15

    Re: Controlling mouse with Python in X

    Thanks for the reply.
    I'm relatively new to Python. By creating a custom wrapper, are you suggesting that I extend my Python code with the C function?

    Where can I start reading about that?

    This is the Xlib code which moves the cursor in C:
    Code:
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    
    void mouseMove(int x, int y)
    {
    	Display *displayMain = XOpenDisplay(NULL);
    
    	if(displayMain == NULL)
    	{
    		fprintf(stderr, "Error Opening the Display !!!\n");
    		exit(EXIT_FAILURE);
    	}
    
    	XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);
    
    	XCloseDisplay(displayMain);
    }
    Regards,
    Nemtaro

  4. #4
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Controlling mouse with Python in X

    Find Wybiral and msg him: he knows I think.

  5. #5
    Join Date
    Apr 2006
    Location
    Phoenix, AZ
    Beans
    251
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Controlling mouse with Python in X

    Quote Originally Posted by nemtaro View Post
    Thanks for the reply.
    I'm relatively new to Python. By creating a custom wrapper, are you suggesting that I extend my Python code with the C function?

    Where can I start reading about that?

    This is the Xlib code which moves the cursor in C:
    Code:
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    
    void mouseMove(int x, int y)
    {
    	Display *displayMain = XOpenDisplay(NULL);
    
    	if(displayMain == NULL)
    	{
    		fprintf(stderr, "Error Opening the Display !!!\n");
    		exit(EXIT_FAILURE);
    	}
    
    	XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);
    
    	XCloseDisplay(displayMain);
    }
    Regards,
    Nemtaro

    Extending Python in C (provides a basic example): http://docs.python.org/ext/ext.html

    The entire API is here: http://docs.python.org/api/api.html
    -Skeeterbug

  6. #6
    Join Date
    Jan 2006
    Location
    Philadelphia
    Beans
    4,076
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Controlling mouse with Python in X

    Quote Originally Posted by nemtaro View Post
    Hi there,
    I need to move the mouse cursor and generate mouse click events in X, and I'm hoping to use Python.

    I understand that the standard Xlib library provides a XWarpPointer function which moves the cursor around. This works for me in C, but I can't seem to find the equivalent function in Python-xlib. Any suggestions would be appreciated

    Also, how would I go about creating a click event once I have the cursor in the right position?

    Regards,
    Nemtaro
    maybe this is what you are looking for? :
    http://python-xlib.sourceforge.net/d..._14.html#SEC13

  7. #7
    Join Date
    Jul 2008
    Beans
    15

    Smile Re: Controlling mouse with Python in X

    Thanks to everyone for your replies, and Wybiral for his help.

    I'm currently compiling the above mentioned C code into a share object, and importing it into Python:
    Code:
    // gcc mouse.c -Wall -c -fPIC -o mouse.o
    // gcc mouse.o -lX11 -shared -Wl,-soname,mouse.so -o mouse.so
    Code:
    from ctypes import cdll, c_int
    
    lib = "./mouse.so"
    
    dll = cdll.LoadLibrary(lib)
    
    mouseMove = (lambda x, y: dll.mouseMove(c_int(x), c_int(y)))
    
    mouseMove(50, 50)
    I'll report back if I manage to get the click events working.

    Cheers,
    Nemtaro

  8. #8
    Join Date
    Feb 2009
    Beans
    4

    Re: Controlling mouse with Python in X

    Any news on the click events?

    I'm still searching for a pure python way to move and click the mouse. I already figured it out for Windows and Mac, Linux is the last missing part...

    I'm writing a module to make life easy for the next one with our problems.
    http://www.python-forum.org/pythonfo...=13825&start=0
    http://code.google.com/p/pymouse/ (send me a PM for access)
    Last edited by pepijndevos; July 12th, 2009 at 01:29 PM.

  9. #9
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Controlling mouse with Python in X

    http://plwm.sourceforge.net/ is a window manager implemented in Python. If you run
    Code:
    apt-get source python-plwm
    you can take a look at its source code. Perhaps you'd find the answer there. (See http://ubuntuforums.org/showpost.php...45&postcount=3)

    Or, if we relax the "pure python" requirement, perhaps a simpler solution would be to study xdotool which is written in C.
    If you install the xdotool package, you can move the mouse with
    Code:
    xdotool mousemove 850 70
    and click with
    Code:
    xdotool  click 1
    xdotool has less lines of code than plwm, so it might be easier to find exactly what you need there.

    Once you find the C code for clicking and moving the mouse, that code could be repackaged as a library with functions callable from Python. (See for example
    http://ubuntuforums.org/showpost.php...47&postcount=7)
    Last edited by unutbu; July 12th, 2009 at 04:19 PM.

  10. #10
    Join Date
    May 2008
    Beans
    9
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Controlling mouse with Python in X

    Quote Originally Posted by nemtaro View Post
    Thanks to everyone for your replies, and Wybiral for his help.

    I'm currently compiling the above mentioned C code into a share object, and importing it into Python:
    Code:
    // gcc mouse.c -Wall -c -fPIC -o mouse.o
    // gcc mouse.o -lX11 -shared -Wl,-soname,mouse.so -o mouse.so
    Code:
    from ctypes import cdll, c_int
    
    lib = "./mouse.so"
    
    dll = cdll.LoadLibrary(lib)
    
    mouseMove = (lambda x, y: dll.mouseMove(c_int(x), c_int(y)))
    
    mouseMove(50, 50)
    I'll report back if I manage to get the click events working.

    Cheers,
    Nemtaro
    A solution which avoids the C compilation step:

    Code:
    from ctypes import cdll
    
    def move_mouse(x,y):
        dll = cdll.LoadLibrary('libX11.so')
        d = dll.XOpenDisplay(None)
        root = dll.XDefaultRootWindow(d)
        dll.XWarpPointer(d,None,root,0,0,0,0,x,y)
        dll.XCloseDisplay(d)
    Last edited by DrMeers; January 14th, 2010 at 04:12 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •