8

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

9

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.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • so for right down do i use (8,0,0,0,0) and (10,0,0,0,0) for right up? – Robert Thackeray Jun 27 '12 at 15:39
  • 1
    `10` and `0x10` are two different things. First is in Base10, and represents decimal 10, and second is in hexadecimal, and represents 16. Except for this, yes, that would invoke a right-click – UltraInstinct Jun 27 '12 at 15:42
  • i just put in >>> import ctypes >>> user32=ctypes.windll.user32 >>> user32.SetCursorPos(650,135), user32.mouse_event(8,0,0,0,0), user32.mouse_event(10,0,0,0,0) and it didnt right click? – Robert Thackeray Jun 27 '12 at 15:52
  • 1
    As I told you, it wont. Try `16` (or `0x10`) instead of `10`. – UltraInstinct Jun 27 '12 at 15:54
  • @Robert Thackeray: Did you see and understand the comment about hex vs decimal numbers? – martineau Jun 27 '12 at 15:55
  • @RobertThackeray Glad that helped. Please read up on the difference between Decimal and hex representation. That will help you in long run :) – UltraInstinct Jun 27 '12 at 15:57