Skip to content

Instantly share code, notes, and snippets.

@zrzka
Last active June 9, 2020 07:53
Show Gist options
  • Save zrzka/692bf7266e551b512a36af80aae176f5 to your computer and use it in GitHub Desktop.
Save zrzka/692bf7266e551b512a36af80aae176f5 to your computer and use it in GitHub Desktop.
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)monitorMouseLocationNSEvent {
void (^printMouseLocation)(NSString *, NSEvent *) = ^(NSString *type, NSEvent *event) {
NSPoint location;
if (event.window == nil) {
location = event.locationInWindow;
} else {
location = [event.window convertPointToScreen:event.locationInWindow];
}
NSLog(@"%.1f x %.1f - NSEvent(%@)", location.x, location.y, type);
};
[NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
handler:^(NSEvent * _Nonnull event) {
printMouseLocation(@"Global", event);
}];
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
printMouseLocation(@"Local", event);
return event;
}];
}
CGEventRef mouseEventTapSession(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
CGPoint p = CGEventGetUnflippedLocation(event);
NSLog(@"%.1f x %.1f - CGEventTap(session, unflipped)", p.x, p.y);
return event;
}
CGEventRef mouseEventTapHID(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
CGPoint p = CGEventGetUnflippedLocation(event);
NSLog(@"%.1f x %.1f - CGEventTap(HID, unflipped)", p.x, p.y);
return event;
}
- (void)tapMouseAtLocation:(CGEventTapLocation)location callback:(CGEventTapCallBack)callback {
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
if (runLoop == NULL) return;
CFMachPortRef machPort = CGEventTapCreate(location, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly,
CGEventMaskBit(kCGEventMouseMoved), callback, nil);
if (machPort == NULL) return;
CFRunLoopSourceRef source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, machPort, 0);
if (source == NULL) return;
CFRunLoopAddSource(runLoop, source, kCFRunLoopCommonModes);
}
- (void)periodicMouseLocation {
CGEventRef event = CGEventCreate(nil);
CGPoint p = CGEventGetUnflippedLocation(event);
CFRelease(event);
NSLog(@"%.1f x %.1f - CGEventGetLocation(periodicMouseLocation)", p.x, p.y);
[self performSelector:@selector(periodicMouseLocation) withObject:nil afterDelay:2.0];
}
- (void)clickMouse {
CGEventTapLocation tapLocation = kCGSessionEventTap;
// My close window button position
CGPoint pt = CGPointMake(250, 530);
CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, pt, kCGMouseButtonLeft);
CGEventPost(tapLocation, theEvent);
CGEventSetType(theEvent, kCGEventLeftMouseUp);
CGEventPost(tapLocation, theEvent);
// CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);
// CGEventSetType(theEvent, kCGEventLeftMouseDown);
// CGEventPost(tapLocation, theEvent);
// CGEventSetType(theEvent, kCGEventLeftMouseUp);
// CGEventPost(tapLocation, theEvent);
CFRelease(theEvent);
NSLog(@"Clicked");
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Get some time to zoom & move the cursor
[self performSelector:@selector(clickMouse) withObject:nil afterDelay:5.0];
[self monitorMouseLocationNSEvent];
// Monitor via CGEventTap
[self tapMouseAtLocation:kCGHIDEventTap callback:mouseEventTapHID];
[self tapMouseAtLocation:kCGSessionEventTap callback:mouseEventTapSession];
[self periodicMouseLocation];
}
@end
//
// Zoom off
//
// 145.9 x 1028.4 - CGEventTap(HID, unflipped)
// 145.9 x 1028.4 - CGEventTap(session, unflipped)
// 145.9 x 1028.4 - NSEvent(Global)
// 145.9 x 1028.4 - CGEventGetLocation(periodicMouseLocation)
//
// Zoom in & viewport moved
//
// -169.1 x 376.1 - CGEventTap(HID, unflipped)
// 291.6 x 512.3 - CGEventTap(session, unflipped)
// 291.6 x 512.3 - NSEvent(Global)
// 291.6 x 512.3 - CGEventGetLocation(periodicMouseLocation)
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment