Controlling mouse with Python

Sasha picture Sasha · Jul 25, 2009 · Viewed 380.1k times · Source

How does one control the mouse cursor in Python, i.e. move it to certain position and click, under Windows?

Answer

Jeffrey Kemp picture Jeffrey Kemp · Jul 25, 2009

Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

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)