0

I have a simple Objective C function used to perform mouse events (single clicks):

void click(CGPoint pt, int clickCount) {
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, pt, kCGMouseButtonLeft);
    CGEventPost(kCGHIDEventTap, theEvent);
    CGEventSetType(theEvent, kCGEventLeftMouseUp);
    CGEventPost(kCGHIDEventTap, theEvent);
    if (clickCount == 2) {
        CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);
        CGEventSetType(theEvent, kCGEventLeftMouseDown);
        CGEventPost(kCGHIDEventTap, theEvent);
        CGEventSetType(theEvent, kCGEventLeftMouseUp);
        CGEventPost(kCGHIDEventTap, theEvent);
    }
    CFRelease(theEvent);
}

The problem is that if the screen is zoomed (Control + Scroll) then the point where it's clicked is not where it should be (I guess it's using the coordinates based on the new viewport, but even so it doesn't make sense).

Is this a bug in the library? Is there anyway to be aware of the user zooming in and still click in the right place, even though if that means moving the screen viewport?

Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72
  • Are you sure that your point is correct? I just tested it and ... 1) Event location (`CGPoint`) does not change when I zoom my screen. 2) Does not depend if a point is on / off screen, works for me. 3) I just tested it and it works as expected in all possible scenarios - (not) zoomed, point on/off screen, ... Tested on macOS Catalina. – zrzka Jun 08 '20 at 12:40
  • I tested it and I see the same issue. My Wacom tablet also clicks in the wrong place. Tested on macOS Catalina and High Sierra. – Willeke Jun 08 '20 at 14:51
  • That's interesting. Tested again, just to be sure, still works as expected. Also tested getting mouse location (`CGEventRef e = CGEventCreate(NULL); CGPoint p = CGEventGetLocation(e); CFRelease(e); NSLog(@"%f x %f", p.x, p.y);`) and it also returns correct location. I run the code periodically, zooming in/out, pointing to one location and then checking numbers. – zrzka Jun 08 '20 at 15:05
  • I'll delete this comment later, but this is my [AppDelegate.m](https://gist.github.com/zrzka/692bf7266e551b512a36af80aae176f5) I'm using for testing - what works for me. – zrzka Jun 08 '20 at 15:18
  • @zrzka Have you tested Full screen Zoom style? – Willeke Jun 08 '20 at 21:38
  • @Willeke yep. Also tested with just internal MBP display, internal MBP display + external display, ... This is my zoom settings - https://cln.sh/e5t8K9 & https://cln.sh/KgN7CR – zrzka Jun 08 '20 at 21:55
  • @zrzka I tried your code, added an event monitor and the posted mousedown event is at a different location when zoomed. Maybe it's screen dependent. – Willeke Jun 08 '20 at 22:25
  • @Willeke okay, I finally reproduced the issue. I was using another method to get a mouse location. Did add event taps and there's a difference. See [updated gist](https://gist.github.com/zrzka/692bf7266e551b512a36af80aae176f5). To make it working, one has to use `kCGSessionEventTap` instead of `kCGHIDEventTap`. – zrzka Jun 09 '20 at 07:55

0 Answers0