Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate Mouse Clicks on Python

I'm currently in the process of making my Nintendo Wiimote (Kinda sad actually) to work with my computer as a mouse. I've managed to make the nunchuk's stick control actually move the mouse up and down, left and right on the screen! This was so exciting. Now I'm stuck.

I want to left/right click on things via python when I press A, When I went to do a search, All it came up with was tkinter?

So my question is, What do I call to make python left/right click on the desktop, and if it's possible, maybe provide a snippet?

Thank you for your help!

NOTE: I guess I forgot to mention that this is for Linux.

like image 566
dbdii407 Avatar asked Aug 23 '10 06:08

dbdii407


People also ask

How do you automate a mouse click in Python?

This code uses moveTo() function, which takes x and y coordinates, and an optional duration argument. This function moves your mouse pointer from it's current location to x, y coordinate, and takes time as specified by duration argument to do so.


1 Answers

You can use PyMouse which has now merged with PyUserInput. I installed it via pip:

  1. apt-get install python-pip

  2. pip install pymouse

In some cases it used the cursor and in others it simulated mouse events without the cursor.

from pymouse import PyMouse  m = PyMouse() m.position() #gets mouse current position coordinates m.move(x,y) m.click(x,y) #the third argument "1" represents the mouse button m.press(x,y) #mouse button press m.release(x,y) #mouse button release 

You can also specify which mouse button you want used. Ex left button:

m.click(x,y,1) 

Keep in mind, on Linux it requires Xlib.

like image 67
Ed Shway Avatar answered Oct 12 '22 01:10

Ed Shway