Tuesday, January 4, 2011

Simulate mouse click and check button press

import win32api
import win32con
import time

#Code to simulate mouse click
x,y = win32api.GetCursorPos()
win32api.SetCursorPos((x, y))
#Left click
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
#Right click
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)

#Code to check if mouse was clicked
a=win32api.GetAsyncKeyState(0x01)
b=win32api.GetAsyncKeyState(0x02)
if a==1:
    # Left button is pressed
else:
    # Left button not pressed
if b==1:
    # Right button pressed 
else:
    # You know

2 comments: