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

Recommended Answers

All 2 Replies

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):
    windll.user32.SetCursorPos(x,y)

def l_click(x="current", y="current"):
    if x == "current" and y == "current":
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    else:
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
        time.sleep(0.05)
        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)
        time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
    else:
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, x, y)
        time.sleep(0.05)
        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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.