Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling mouse events on transparent window conditionally

I am developing an Desktop application in which I should be able to take mouse events on transparent window. But, transparent NSWindow does not take mouse events. So, I have set setIgnoreMouseEvents to NO which allows the transparent window to take mouse events.

I have the problem in the following scenario: There is dynamically created rectangular shape on this window. The transparent window should not take mouse events in this region; it should be delegated to the window (of some other app) that is present behind this shape. For this purpose, if the mouseDown event is inside the shape I am setting setIgnoreMouseEvents to YES. Now, if the user performs mouse events in the area outside the shape the transparent window should take the event. Since, setIgnoreMouseEvents is set to YES, window does not take mouse events.

There is no way to identify that mouseDown event has occurred so that I can set setIgnoreMouseEvents to NO.

Could someone suggest me some best method to handle mouse events on transparent window?

Deepa

like image 534
spd Avatar asked May 31 '11 06:05

spd


1 Answers

I've just come across Quartz Event Taps, which basically let you capture the mouse event and execute your own callback.

Haven't tried it out myself, but it seems like you should be able to check where the mouse click fell and execute conditionally on the values

Here's 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: %d y:%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);  
 }  
like image 167
rapidex Avatar answered Sep 23 '22 14:09

rapidex