如何检测任务控制或Command-Tab切换器取代OS X中的程序? [英] How can one detect Mission Control or Command-Tab switcher superseding one's program in OS X?

查看:160
本文介绍了如何检测任务控制或Command-Tab切换器取代OS X中的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在程序中使用CGAssociateMouseAndMouseCursorPosition(NO)。这会在应用程序处于前台时断开鼠标与屏幕上光标的连接。不幸的是,它也会在任务控制或应用程序切换器或知道什么发生时断开连接。

I'm trying to use CGAssociateMouseAndMouseCursorPosition(NO) in a program. This disconnects the mouse from the on screen cursor when your application is "in the foreground". Unfortunately it also disconnects it when Mission Control or the application switcher or who knows what else comes up.

到目前为止,我知道:


  • 应用程式仍然有效。

  • 窗口仍然是键。


  • 应用程序停止接收鼠标移动的事件,但NSEvent addGlobalMonitorForEventsMatchingMask:handler:也不会接收它们,这是奇怪的。它应该接收任何未传递给我的应用程序的事件。 (我计划检测丢失的事件,以便知道何时再次关联鼠标。

  • The application is still active.
  • The window is still key.
  • Nothing is sent to the default notification center when these things come up.
  • The application stops receiving mouse moved events, but an NSEvent addGlobalMonitorForEventsMatchingMask:handler: also does not receive them, which is strange to say the least. It should receive any events not delivered to my application. (I was planning to detect the missing events to know when to associate the mouse again.

因此,有一种方法可以检测我的应用程序不再受控制,特别是因为任务控制或开关已经接管了?他们真的期望鼠标工作,我需要恢复它们的关联。

So, is there a way to detect when my application is no longer in control, specifically because Mission Control or the switch has taken over? They really expect the mouse to work and I need to restore that association for them.

推荐答案

我有一个惊喜,全局事件监视器没有看到事件。在类似的情况下,我使用了 Quartz Event Tap 用于类似目的Cocoa全局事件监视器与事件触发非常相似,因此

I share your surprise that a global event monitor isn't seeing the events. In a similar situation, I used a Quartz Event Tap for a similar purpose. The Cocoa global event monitor is quite similar to event taps, so I figured it would work.

我点击 kCGAnnotatedSessionEventTap ,比较了 CGEventGetIntegerValueField(event,kCGEventTargetUnixProcessID) getpid(),以确定事件何时到达另一个应用任务控制或Exposé)。 (当我的应用程式重新启用活动状态时,我会停用该标签,因此只有在显示此类重叠式使用者介面时,才会接收指定给其他应用程式的活动)。

I put the tap on kCGAnnotatedSessionEventTap and compared the result from CGEventGetIntegerValueField(event, kCGEventTargetUnixProcessID) to getpid() to determine when the events were going to another app (e.g. Mission Control or Exposé). (I disable the tab when my app resigns active status, so it should only receive events destined for another app when this sort of overlay UI is presented.)

顺便说一下,你提到监视默认通知中心,但是,如果有关于任务控制等的通知,它更有可能来到分布式通知中心( NSDistributedNotificationCenter )。因此,值得检查。

By the way, you mentioned monitoring the default notification center, but, if there's a notification about Mission Control or the like, it's more likely to come to the distributed notification center (NSDistributedNotificationCenter). So, it's worth checking that.

这篇关于如何检测任务控制或Command-Tab切换器取代OS X中的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆