Home > Software engineering >  Simulating a kCGEventOtherMouseDown only works for right-clicking
Simulating a kCGEventOtherMouseDown only works for right-clicking

Time:10-27

The following code for right-clicking works:

auto event = CGEventCreateMouseEvent(nullptr, kCGEventOtherMouseDown,
                                     {x, y}, kCGMouseButtonRight);
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);

however, the exact same code with kCGMouseButtonRight replaced with kCGMouseButtonLeft doesn't.

Sidenote: I have wondered if this is due to the latter event not registering, so I tried a "Key Test" and found out that both events register as the middle-button(although the right-click works as expected for other applications).

Why does the left-click not work and how can I make it?

CodePudding user response:

I was able to solve this by using kCGEventLeftMouseDown instead of kCGEventOtherMouseDown. This goes in hand with the Apple documentation of kCGEventOtherMouseDown:

Specifies a mouse down event with one of buttons 2-31.

(left = 0, right = 1, center = 2, extra buttons = 3-31)

This also explains why the website registered left & right as center, but it does not explain why the right-click still worked(opened the context menu), but I guess that's what you gotta deal with when not looking at the docs properly.

  • Related