如何监控系统范围内的用户活动?
如何实现某种后台进程来不断(或以短时间间隔)检查用户是否正在与系统交互,即简单明了地使用计算机?最明显的方法是检查用户生成的任何类型的事件,例如按键、鼠标移动和鼠标单击等。在某个应用程序中捕获事件很简单,因为这些事件通常是通过应用程序的事件循环自动提供的,但如何在系统范围内实现这一目标,即无论哪个应用程序位于前面/具有焦点?这是如何优雅地完成的,以便这样的过程不会消耗太多的系统资源?
我对此很感兴趣,但显然每个平台都有不同的方式——跨平台方式(Java)是理想的,但我选择的平台是Mac OS X(Cocoa)。
How would one implement some kind of background process that constantly (or in short intervals) checks if the user is interacting with the system, that is, plain and simply, using the computer? The obvious way is to check for user-generated events of any kind, such as key presses, mouse moves and mouse clicks, etcetera. It is straightforward to capture events within a certain application because those are usually automatically supplied via the application's event loop, but how does one achieve this system-wide, i.e. irrespective of what application is in front/has focus? How is this done elegantly, so such a process does not consume too much system resources?
I'm interested in this in general, but obviously there is a different way for each platform – a cross-platform way (Java) would be ideal, but my platform of choice is Mac OS X (Cocoa).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Thomas Langston 提出的检查 pidgin (libpurple) 如何实现的想法非常有帮助。我下载了源代码并发现了这个 -
在 pidgin-2.10.0/pidgin/gtkidle.c 从第 46 行开始:
该文件包含为不同平台处理此问题的代码。
The idea by Thomas Langston to check out how pidgin (libpurple) does it was very helpful. I downloaded the source code and found this --
In pidgin-2.10.0/pidgin/gtkidle.c starting on line 46:
That file contains the code to handle this for the different platforms.
我不知道明确的答案,但我知道我要去哪里寻找。 Pidgin 和其他开源 IM 客户端必须知道用户是否空闲。我确信您可以使用类似的方法来确定用户活动。
I don't know the explicit answer, but I know where I'd go to look. Pidgin and other open source IM clients have to know if the user is idle. I'm sure you could use a similar method to determine user activity.
捕获诸如按键或鼠标移动之类的系统事件不是任何一种语言的领域。这基本上是操作系统管理的内容,并且因为您需要某种机制来侦听系统范围的事件,所以您必须以一种或另一种方式依赖操作系统提供的 API。例如,在 Windows 上,您可以获得 Win API,可以在 Java 程序中使用它来侦听系统范围的事件。但这将特定于 Win API,因此对于 Mac OS,它将是不同版本的 API。
Capturing a system event like a key press or mouse movement is not the realm of any one language as such. This is basically what the OS manages, and because you want some mechanism which has to listen to system-wide events , you must depend on OS provided API in one or another way. For example , on Windows ,you get the Win API that can be used from within a Java program to listen to system wide events. But this will be specific to Win API, so for Mac OS , it will be a different version of API.