在Cocoa应用程序中检测用户活动(轻击,点击等) [英] Detect user activity in Cocoa app (taps, clicks, ...)
问题描述
对于Mac应用程序,我想检测该应用程序中的用户活动,因此可以定期让Web服务知道该用户在端点上仍处于活动状态.
在Cocoa Touch中,我会覆盖UIApplication
的sendEvent
,但是可可中NSApplication
中的sendEvent
等效,
对于Mac应用程序,我应该使用哪些API来检测用户活动?也许我可以通过某种方式在全球范围内建立响应者联播,从那里可以将ping发送到我的服务?
最好,我想听用户期望每15-30秒执行一次的操作.单击,标签,键入,切换窗口或应用程序.
您最有可能希望创建 +[NSEvent addGlobalMonitorForEventsMatchingMask:handler:]
.每当类型与传递的掩码(您应使用NSAnyEventMask
)匹配的事件发送到另一个应用程序时,此方法都会调用您的处理程序.您可以在此处观察但不能更改该事件,它非常适合您的用法.需要注意的一件事:文档说,除非您的应用程序具有可访问性,否则您将不会收到关键事件.
对于通过 解决方案
You most likely want to create a global event monitor using +[NSEvent addGlobalMonitorForEventsMatchingMask:handler:]
. This calls your handler whenever an event whose type matches the passed mask (you should use NSAnyEventMask
) is sent to another application. You can observe, but not change, the event here, which suits your usage perfectly. There is one thing to watch out for: the documentation says that you won't receive key events unless your app is trusted for Accessibility.
You can do similarly for events that are routed to your own application with +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]
.
这篇关于在Cocoa应用程序中检测用户活动(轻击,点击等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!