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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

so for right down do i use (8,0,0,0,0) and (10,0,0,0,0) for right up?
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
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?
As I told you, it wont. Try 16 (or 0x10) instead of 10.
@Robert Thackeray: Did you see and understand the comment about hex vs decimal numbers?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.