如何在没有焦点的情况下让NSWindow处理mouseDown事件?

时间:2022-02-13 00:01:30

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?

现在我有了一个无边界窗口,它处理鼠标向下事件来移动和调整自己的大小。但是,如何在没有焦点的情况下处理鼠标向下事件呢?

2 个解决方案

#1


6  

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

您的自定义视图必须实现-acceptsFirstMouse:方法并返回YES。

#2


2  

[NSWindow windowNumberAtPoint:mouseDownCoordinates belowWindowWithWindowNumber:0];

[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.

传入mousedowncoordinate,您将通过Quartz事件服务捕获它。它会返回鼠标悬停的窗口号。抓住那个窗口,进行移动/调整大小。

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;
}

#1


6  

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

您的自定义视图必须实现-acceptsFirstMouse:方法并返回YES。

#2


2  

[NSWindow windowNumberAtPoint:mouseDownCoordinates belowWindowWithWindowNumber:0];

[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.

传入mousedowncoordinate,您将通过Quartz事件服务捕获它。它会返回鼠标悬停的窗口号。抓住那个窗口,进行移动/调整大小。

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;
}