我正在开发一个桌面应用程序,我应该能够在透明窗口上获取鼠标事件。但是,透明的 NSWindow 不接受鼠标事件。因此,我将 setIgnoreMouseEvents 设置为 NO,它允许透明窗口接收鼠标事件。
我在以下情况下遇到问题:
此窗口上有动态创建的矩形形状。透明窗口不应在该区域接受鼠标事件;它应该委托(delegate)给这个形状后面的窗口(其他一些应用程序)。
为此,如果 mouseDown 事件在我将 setIgnoreMouseEvents 设置为 YES 的形状内。现在,如果用户在形状之外的区域执行鼠标事件,透明窗口应该接受该事件。由于 setIgnoreMouseEvents 设置为 YES,因此窗口不接受鼠标事件。
无法识别 mouseDown 事件已发生,因此我可以将 setIgnoreMouseEvents 设置为 NO。
有人可以建议我一些在透明窗口上处理鼠标事件的最佳方法吗?
迪帕
最佳答案
我刚刚遇到了 Quartz Event Taps,它基本上可以让你捕获鼠标事件并执行你自己的回调。
我自己还没有尝试过,但似乎你应该能够检查鼠标点击的位置并根据值有条件地执行
这是 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);
}
关于macos - 有条件地处理透明窗口上的鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6183594/