Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Click in Python using ctypes

I am a complete beginner to Python so dont understand the lingo. I want to use python to do a simple click at a specific point. I have already managed a left click using ctypes:

>>> import ctypes
>>> ctypes.windll.user32.SetCursorPos(x,y), ctypes.windll.user32.mouse_event(2,0,0,0,0), ctypes.windll.user32.mouse_event(4,0,0,0,0)

is there a way to do a right click in the same way?

like image 725
Robert Thackeray Avatar asked Jun 27 '12 15:06

Robert Thackeray


1 Answers

Here are the constants that you would use for mouse_event

MOUSE_LEFTDOWN = 0x0002     # left button down 
MOUSE_LEFTUP = 0x0004       # left button up 
MOUSE_RIGHTDOWN = 0x0008    # right button down 
MOUSE_RIGHTUP = 0x0010      # right button up 
MOUSE_MIDDLEDOWN = 0x0020   # middle button down 
MOUSE_MIDDLEUP = 0x0040     # middle button up 

In your code you are sending two events: MOUSE_LEFTDOWN and MOUSE_LEFTUP. That simulates a "click".

Now for a right click you would send MOUSE_RIGHTDOWN and MOUSE_RIGHTUP in a similar fashion.

like image 99
UltraInstinct Avatar answered Oct 17 '22 03:10

UltraInstinct