42

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
dbdii407
  • 815
  • 3
  • 11
  • 14
  • 2
    This will be OS/windowing system dependent probably. Ideally, there will be python bindings to your operating systems windowing system. Normally, the arguments are pretty similar to the C/C++/C#/Objective-C calls that the documentation is actually written in. Or you might luck out and have actual python docs. At least post what OS you're on so people can help. – aaronasterling Aug 23 '10 at 06:48
  • Probably this answer is not relevant but maybe you could check Sikuli project source. It is jython but... – joaquin Aug 23 '10 at 07:42
  • Looks like a nice project.. what are you using to move the mouse? Are you doing that via Python, or in another way? And, of course, on which OS/DE/WM? Btw, some times ago, I found a Xorg driver that should allow to use the Wiimote as a mouse (for the ir-pen interactive whiteboard project), maybe it could do the job..? – redShadow Aug 23 '10 at 11:27
  • I'm moving the mouse with libX11 on python. I can't use IR so this is why i need to be able to click. – dbdii407 Aug 23 '10 at 18:05
  • 2
    http://github.com/msanders/autopy#readme This came up on another question and it simulates mouseclicks. Maybe you can look and see how they do it. – aaronasterling Aug 24 '10 at 12:42

6 Answers6

34

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.

Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
Ed Shway
  • 502
  • 6
  • 7
4

The evdev package provides bindings to parts of the input handling subsystem in Linux. It also happens to include a pythonic interface to uinput.

Example of sending a relative motion event and a left mouse click with evdev:

from evdev import UInput, ecodes as e

capabilities = {
    e.EV_REL : (e.REL_X, e.REL_Y), 
    e.EV_KEY : (e.BTN_LEFT, e.BTN_RIGHT),
}

with UInput(capabilities) as ui:
    ui.write(e.EV_REL, e.REL_X, 10)
    ui.write(e.EV_REL, e.REL_Y, 10)
    ui.write(e.EV_KEY, e.BTN_LEFT, 1)
    ui.syn()
gvalkov
  • 3,977
  • 30
  • 31
4

PyAutoGui works superb.. Thanks to Al Sweigart...

An example of mine...

import pyautogui

pyautogui.FAILSAFE = False

for x in range(555, 899):
    pyautogui.moveTo(x, x)
2

You can install the PyAutoGUI GUI automation module from PyPI (run pip install pyautogui) and then call the pyautogui.click() to click on a certain X and Y coordinates of the screen:

>>> import pyautogui
>>> pyautogui.click(50, 100)
>>> pyautogui.moveTo(200, 200)

PyAutoGUI works on Windows, Mac, and Linux, and on Python 2 and 3. It also can emulate the keyboard, do mouse drags, take screenshots, and do simple image recognition of the screenshots.

Full docs are at https://pyautogui.readthedocs.org/

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
2

Open your terminal and goto cd /usr/share/pyshared/twisted/protocols/mice
may this __init__.py mouseman.py python script will work for you,check them out.

-1

I didn't see this mentioned, so here it goes - there is also python-dogtail; see:

It requires "Enable assistive technologies" in the Gnome Desktop - but can in principle obtain e.g. names of GUI buttons of an application, and allow virtual clicks on them (rather than via x/y coordinates).

sdaau
  • 36,975
  • 46
  • 198
  • 278