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?


I don't know what "X" is but, I do knoe how to click and move the mouse in python, you will need PyWin to do it. You simple need to use win32api like so.

import win32api
import win32con
from ctypes import windll
import time

def m_move(x,y):

def l_click(x="current", y="current"):
    if x == "current" and y == "current":
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y)

def r_click(x="current", y="current"):
    if x == "current" and y == "current":
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, x, y)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, x, y)

That will let you move the mouse, or, move the mouse and click at the same time which is way more efficient. I just save this file as winmouse.py and import it so my scripts dont get to big.

This article has been dead for over six months. Start a new discussion instead.