Processing mouse events on a transparent window conditionally

I am developing a desktop application in which I should be able to receive mouse events in a transparent window. But transparent NSWindow does not accept mouse events. So, I set setIgnoreMouseEvents to NO, which allows the transparent window to accept mouse events.

I have a problem in the following scenario: This window has a dynamically created rectangular shape. A transparent window should not accept mouse events in this region; it must be delegated to the window (of some other application) that is present behind this form. For this purpose, if the mouseDown event is inside the form, I set setIgnoreMouseEvents to YES. Now, if the user executes mouse events in an area outside the form, the transparent window should accept the event. Because setIgnoreMouseEvents is set to YES, the window does not accept mouse events.

It is not possible to identify the mouseDown event so that I can set setIgnoreMouseEvents to NO.

Can someone suggest me a better method for handling mouse events in a transparent window?

Deepa

+8
conditional transparent mouseevent macos nswindow
source share
1 answer

I just met with Quartz Event Taps, which basically allows you to capture a mouse event and execute your own callback.

I havenโ€™t tried it myself, but it looks like you should check where you clicked the mouse and conditionally execute on the values

Here is an example :

//--------------------------------------------------------------------------- CGEventRef MouseTapCallback( CGEventTapProxy aProxy, CGEventType aType, CGEventRef aEvent, void* aRefcon ) //--------------------------------------------------------------------------- { if( aType == kCGEventRightMouseDown ) NSLog( @"down" ); else if( aType == kCGEventRightMouseUp ) NSLog( @"up" ); else NSLog( @"other" ); CGPoint theLocation = CGEventGetLocation(aEvent); NSLog( @"location x: %dy:%d", theLocation.x, theLocation.y ); return aEvent; } //--------------------------------------------------------------------------- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification //--------------------------------------------------------------------------- { CGEventMask theEventMask = CGEventMaskBit(kCGEventRightMouseDown) | CGEventMaskBit(kCGEventRightMouseUp); CFMachPortRef theEventTap = CGEventTapCreate( kCGSessionEventTap, kCGHeadInsertEventTap, 0, theEventMask, MouseTapCallback, NULL ); if( !theEventTap ) { NSLog( @"Failed to create event tap!" ); } CFRunLoopSourceRef theRunLoopSource = CFMachPortCreateRunLoopSource( kCFAllocatorDefault, theEventTap, 0); CFRunLoopAddSource( CFRunLoopGetCurrent(), theRunLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(theEventTap, true); } 
+3
source share

All Articles