2

I am trying to find the answer to a topic which has basically driven me insane. Is there a way to capture the mouse pointer much like Apple's screencapture utility works? What I am trying to do requires me to draw a clear window that does not activate my app but instead overlays everything, including the dock. The only software I have seen do this is screencapture by utilizing command+shift+4.

I have tried creating a non-activating NSPanel but it doesn't work with cursor rects unless my app is active (which I don't want it to be). Also the problem with NSPanel is that if I am in Dashboard I cannot activate the window on it.

The requirements for what I am trying to do are this. • My app must not activate (keep whatever active app as active) • Must be able to change NSCursor • Mouse does not trigger dock hiding/unhiding (or any other event) • Work even in dashboard

I have tried this code before but its limitations are the same as listed above.

NSScreen * s = [NSScreen mainScreen];

[self.window setStyleMask:NSNonactivatingPanelMask];
[self.window setHasShadow:NO];
[self.window setOpaque:NO];
[self.window setBackgroundColor:[NSColor clearColor]];
[self.window setFrame:s.frame display:NO];
[self.window orderFront:self];
[self.window setLevel:NSMainMenuWindowLevel + 1];
[self.trackingView addCursorRect:self.window.frame cursor:[NSCursor closedHandCursor]];
MAH
  • 841
  • 6
  • 16
  • I can't believe someone else is having the exact same problem as me! It's been driving me up the wall. An app called Snappy seems to have done it (they're distributed on the appstore so I imagine they're sandboxed), but I'm not sure how. https://apps.apple.com/us/app/snappy-snapshots-the-smart-way/id512617038?mt=12 – Salem Jun 23 '22 at 02:53
  • I just posted a duplicate of this a few days ago as well. https://stackoverflow.com/questions/72687979/how-can-i-implement-the-native-macos-screenshot-mouse-behavior – Salem Jun 23 '22 at 02:53

1 Answers1

2

Turns out that using CGEventTap and controlling mouse events before they are distributed to any app works. Unfortunately if you have a Sandboxed app then you can't use CGEventTap. Anyone know if there is a way to gain the same functionality with sandboxing?

MAH
  • 841
  • 6
  • 16