问题
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]];
回答1:
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?
来源:https://stackoverflow.com/questions/19646681/cocoa-capture-mouse-just-like-screencapture-utility