Prevent application activation when clicking on NSWindow / NSView

I am working on a Mac OS application. I am trying to rebuild what happens when you press Cmd-Ctrl-Shift-4: the cross cursor and the selection rectangle for the screenshot.

I use custom borderless NSWindow for all other windows. I disabled the cursor to draw it along with the selection rectangle.

My problem is that as soon as I click and drag to take a screenshot, my application is activated (because the click is intercepted by my screen window).

Is there a way to get a click in my user view / window without activating my application?

I tried using NSPanelwith a flag NSNonactivatingPanelMask, but in this case I have a cursor problem: I cannot draw my own when another application is active, because I cannot hide the cursor for other applications ...

+4
source share
3 answers

In fact, I have a new, better answer to this question related to a large number of undocumented goodies. Here it is for future offspring:

NSWindow has an undocumented method that does exactly what you want:

@interface NSWindow (Private)
- (void )_setPreventsActivation:(bool)preventsActivation;
@end

[myWindow _setPreventsActivation:true];

This stops the window from activating both itself and its application when the user clicks on it.

API, , : Apple - ( OS X, , ), Mac.

+1

, , - , , / , CGEventTap (. Quarts Event Services documentation) ( ). , NSEvent .

( ) , , root, . , , , .

, NSEvent ; 10.6... , , , .

0

, , . API, , :

extern "C" {
    typedef int CGSConnection;
    void CGSSetConnectionProperty(int, int, const void *, const void *);
    int CGSMainConnectionID();
}

void allowHidingCursorForBackgroundOnlyApp()
{
    CFStringRef propertyString = CFStringCreateCopy(NULL, CFSTR("SetsCursorInBackground"));
    CGSSetConnectionProperty(CGSMainConnectionID(), CGSMainConnectionID(), propertyString, kCFBooleanTrue);
    CFRelease((CFTypeRef)propertyString);
}

, , .

0

All Articles