Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

automating mouse movementson Linux X (or via VNC)

Posted on 2010-08-29
7
Medium Priority
?
809 Views
Last Modified: 2012-05-10
From a python script I'd like to be able to move the mouse to certain
absolute coordinates on the screen.


There's no problems calling an external program with subprocess.popen,
as I do not want to perform many movements.

The mouse can jump it doesn't have to visibly move to the target coordinate.



What would you suggest to achieve this on Linux Ubuntu 10.4?

Lateron it would be intersting to acheive the same on a Windows PC


One idea, that I had (for a cross platform solution) would be to start a
VNC server on localhost and the current display and run a small custom
VNC client, which will only control the mouse.

However I have no idea how easy it would be to use a custom VNC client
for moving the mouse.


I read about python-vnc-viewer but don't know how complex it would be to
use it as a base for 'just moving the mouse'



Thanks for any suggestions and ideas







0
Comment
Question by:gelonida
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 13

Assisted Solution

by:Superdave
Superdave earned 800 total points
ID: 33553593
Probably the best way from Python would be to install the Xlib library for Python, create a display, and call its warp_pointer(x,y) method.  Looks like it should be simple except I tried it and it didn't work, so it might require some more research or experimenting (or maybe my version is just too old).  If there is a way to do it with Tkinter it would be better and more portable, but I can't find a way.  If there were it would be cross-platform though.
0
 
LVL 17

Author Comment

by:gelonida
ID: 33553829
Hi Superdave,

I'm not sure, that Tkinter can click/move the mouse outside of its own scope.

Meanwhile somebody else pointed me to a Linux only solution, which works quite well for slow mouse
moves / clicks  etc.

The tool is xte ( "xte - Generates fake input using the XTest extension" )

the command is
to move the mouse cursor to 100,200 one would call
xte 'mousemove 200 200'


What would be interesting now would be either
- solutions without subprocess.Popen
or
soultions working on windows AND Linux

0
 
LVL 5

Accepted Solution

by:
-Richard- earned 1200 total points
ID: 33557679
The best solution by far in the Windows environment is Mark Hammond's "PyWin32", which essentially offers a complete Python interface to the Windows SDK.   Anything you could do by programming directly to the Windows system APIs in C or C++ is easily available directly from any Python program, with cursor movement being the least of its capabilities.   It can be downloaded from here:  http://sourceforge.net/projects/pywin32/  and once installed, the following code should get you started.  

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

Unfortunately, this option only works under Windows.  I'm not aware off the top of my head of a good cross-platform solution,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Author Comment

by:gelonida
ID: 33557803
Hi Richard,

Thanks. This works perfectly for Windows.

So now I have a:
- a solution with a library for windows
- a solution with subprocess.Popen for Linux

There's potentially also Superdave's solution for X-Windows, but if I understood him correctly not working as expected.

I'll wait a little more to see if anybody knows a tiny Linux code snippet for Linux (without subprocess).
Then I'll close this issue

0
 
LVL 5

Expert Comment

by:-Richard-
ID: 33557955
I have done some more research and have concluded, sadly, that there is truly no canned "out of the box" cross platform solution to this problem.   The best solution in the Linux environment is the X solution suggested by Superdave.  X is a *nux-only solution and only if your *nix is running X-Windows - I supect that Superdave tried running it under Windows and that's why he had no success.
0
 
LVL 17

Author Closing Comment

by:gelonida
ID: 33558476
Thanks

Having now two solutions which work nicely on Linux / Windows
0
 
LVL 17

Author Comment

by:gelonida
ID: 33558497
Hmm, Somehow I failed attaching a working code snippet for X.


So here it is again.

I thought it might interesting for the ones searching for this question.


#!/usr/bin/env python
import os
import Xlib
import Xlib.X
import Xlib.display
import time

import Xlib.display
import Xlib.ext.xtest

class MouseControl(object):
    def __init__(self):
        DISPLAY=os.environ.get('DISPLAY')
        self.display = Xlib.display.Display(DISPLAY)
        self.screen = self.display.screen()
        self.root = self.screen.root

    def mouse_click(self, button):
        self.mouse_down(button)
        self.mouse_up(button)

    def mouse_down(self, button): #button= 1 left, 2 middle, 3 right
        Xlib.ext.xtest.fake_input(self.display,Xlib.X.ButtonPress, button)
        self.display.sync()

    def mouse_up(self, button):
        Xlib.ext.xtest.fake_input(self.display,Xlib.X.ButtonRelease, button)
        self.display.sync()

    def mouse_warp(self, x,y):
        self.root.warp_pointer(x,y)
        self.display.sync()

    def get_screen_resolution(self):
        return self.screen['width_in_pixels'], self.screen['height_in_pixels']

def main():
    mc = MouseControl()
    for i in range(4):
        time.sleep(0.5)
        mc.mouse_warp(i*100,i*100)
    mc.mouse_click(3)
    

if __name__ == "__main__":
    main()

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question