Objective c 如何使NSWindow在没有焦点的情况下处理mouseDown事件?
现在我有了一个无边界窗口,可以处理鼠标下降事件以移动和调整自身大小。但是如何处理没有焦点的鼠标下降事件呢?Objective c 如何使NSWindow在没有焦点的情况下处理mouseDown事件?,objective-c,cocoa,nswindow,Objective C,Cocoa,Nswindow,现在我有了一个无边界窗口,可以处理鼠标下降事件以移动和调整自身大小。但是如何处理没有焦点的鼠标下降事件呢?[NSWindow windownumberpoint:mousedown坐标低于windowwithwindownumber:0] 传递mousedown坐标in,您可以通过该坐标进行捕获。它将返回鼠标悬停的窗口号。抓住那个窗口,进行移动/调整大小 示例实现(主要取自): #导入 //所需的全局/IVAR: //1)CGEventTap eventTap是ivar或其他全局 //2)NSI
[NSWindow windownumberpoint:mousedown坐标低于windowwithwindownumber:0]代码>
传递mousedown坐标
in,您可以通过该坐标进行捕获。它将返回鼠标悬停的窗口号。抓住那个窗口,进行移动/调整大小
示例实现(主要取自):
#导入
//所需的全局/IVAR:
//1)CGEventTap eventTap是ivar或其他全局
//2)NSInteger(或int)myWindowNumber是窗口编号
//无边界窗口的编号
void createEventTap(void)
{
CFRunLoopSourceRef runLoopSource;
CGEventMask eventMask=NSLeftMouseDownMask;//mouseDown事件
//创建事件点击
eventTap=CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap,//在其他事件点击之前触发
kCGEventTapOptionDefault,
事件掩码,
myCGEventCallback,//事件触发时接收的回调
零);
//创建一个运行循环源。
runLoopSource=
CFMachPortCreateRunLoopSource(kCFAllocatorDefault,eventTap,0);
//添加到当前运行循环。
CFRunLoopAddSource(CFRunLoopGetCurrent(),
runLoopSource,
KCFRUNLOP模式);
//启用事件点击。
CGEventTapeEnable(eventTap,true);
}
//执行繁重工作的CGEvent回调
CGEventRef myCGEventCallback(CGEventTapProxy代理,CGEventType类型,CGEventRef theEvent,void*refcon)
{
//在这里处理事件
如果([NSWindow WindowNumberPoint:CGEventGetLocation(事件)
belowIndowWithWindowNumber:0]==myWindowNumber)
{
//现在我们知道我们的窗口就是光标下的窗口
}
//如果此时进行移动/调整大小,
//然后返回NULL以防止发生任何其他情况
//从对事件的反应来看,
//否则返回事件。
返回事件;
}
您的自定义视图必须实现-acceptsFirstMouse:
方法并返回YES
这对于您自己的应用程序中的窗口不是必需的。您只需要实现-acceptsFirstMouse:
,就像我的答案一样。哦,好吧,希望它能帮助别人!
#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;
}