3

Now i have a borderless window which handle the mouse down event to move and resize itself.But how can i handle the mouse down event without focus?

NeXT5tep
  • 861
  • 1
  • 11
  • 23

2 Answers2

7

Your custom view must implement the -acceptsFirstMouse: method and return YES.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
2

[NSWindow windowNumberAtPoint:mouseDownCoordinates belowWindowWithWindowNumber:0];

Pass mouseDownCoordinates in, which you would capture via Quartz Event Services. It'll return the window number which the mouse is hovering over. Grab that window and do your move/resize.

Sample implementation (mostly taken from here):

#import <ApplicationServices/ApplicationServices.h>

// Required globals/ivars:
// 1) CGEventTap eventTap is an ivar or other global
// 2) NSInteger (or int) myWindowNumber is the window
//    number of your borderless window

void createEventTap(void)
{
 CFRunLoopSourceRef runLoopSource;

 CGEventMask eventMask = NSLeftMouseDownMask; // mouseDown event

 //create the event tap
 eventTap = CGEventTapCreate(kCGSessionEventTap,
            kCGHeadInsertEventTap, // triggers before other event taps do
            kCGEventTapOptionDefault,
            eventMask,
            myCGEventCallback, //the callback we receive when the event fires
            nil); 

 // Create a run loop source.
 runLoopSource = 
   CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

 // Add to the current run loop.
 CFRunLoopAddSource(CFRunLoopGetCurrent(),
                    runLoopSource,
                    kCFRunLoopCommonModes);

 // Enable the event tap.
 CGEventTapEnable(eventTap, true);
}


//the CGEvent callback that does the heavy lifting
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef theEvent, void *refcon)
{
 // handle the event here
 if([NSWindow windowNumberAtPoint:CGEventGetLocation(theEvent)
     belowWindowWithWindowNumber:0] == myWindowNumber)
 {
   // now we know our window is the one under the cursor
 }

 // If you do the move/resize at this point,
 // then return NULL to prevent anything else
 // from responding to the event,
 // otherwise return theEvent.

 return theEvent;
}
Community
  • 1
  • 1