有条件地处理透明窗口上的鼠标事件

发布于 2024-11-11 15:31:42 字数 443 浏览 3 评论 0原文

我正在开发一个桌面应用程序,其中我应该能够在透明窗口上获取鼠标事件。但是,透明 NSWindow 不接受鼠标事件。因此,我将 setIgnoreMouseEvents 设置为 NO,这允许透明窗口接收鼠标事件。

我在以下场景中遇到问题: 该窗口上有动态创建的矩形形状。透明窗口不应在该区域接收鼠标事件;它应该委托给该形状后面的窗口(某些其他应用程序的窗口)。 为此,如果 mouseDown 事件位于形状内部,我将 setIgnoreMouseEvents 设置为 YES。现在,如果用户在形状外部的区域中执行鼠标事件,透明窗口应该接收该事件。由于 setIgnoreMouseEvents 设置为 YES,窗口不接受鼠标事件。

无法识别 mouseDown 事件是否发生,因此我可以将 setIgnoreMouseEvents 设置为 NO。

有人可以建议我一些处理透明窗口上的鼠标事件的最佳方法吗?

迪帕

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2024-11-18 15:31:42

我刚刚遇到了 Quartz Event Taps,它基本上可以让您捕获鼠标事件并执行您自己的回调。

我自己还没有尝试过,但似乎您应该能够检查鼠标单击的位置并根据值有条件地执行

这是一个 示例

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

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);  
 }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文