如何监控系统范围内的用户活动?

发布于 2024-12-01 08:43:22 字数 273 浏览 1 评论 0原文

如何实现某种后台进程来不断(或以短时间间隔)检查用户是否正在与系统交互,即简单明了地使用计算机?最明显的方法是检查用户生成的任何类型的事件,例如按键、鼠标移动和鼠标单击等。在某个应用程序中捕获事件很简单,因为这些事件通常是通过应用程序的事件循环自动提供的,但如何在系统范围内实现这一目标,即无论哪个应用程序位于前面/具有焦点?这是如何优雅地完成的,以便这样的过程不会消耗太多的系统资源?

我对此很感兴趣,但显然每个平台都有不同的方式——跨平台方式(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 技术交流群。

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

发布评论

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

评论(3

一紙繁鸢 2024-12-08 08:43:22

Thomas Langston 提出的检查 pidgin (libpurple) 如何实现的想法非常有帮助。我下载了源代码并发现了这个 -

在 pidgin-2.10.0/pidgin/gtkidle.c 从第 46 行开始:

/*
* Get the number of seconds the user has been idle.  In Unix-world
* this is based on the X Windows usage.  In MS Windows this is
* based on keyboard/mouse usage information obtained from the OS.
* In MacOS X, this is based on keyboard/mouse usage information
* obtained from the OS, if configure detected IOKit.  Otherwise,
* MacOS X is handled as a case of X Windows.
*
* In Debian bug #271639, jwz says:
*
* Purple should simply ask xscreensaver how long the user has been idle:
*   % xscreensaver-command -time
*   XScreenSaver 4.18: screen blanked since Tue Sep 14 14:10:45 2004
*
* Or you can monitor the _SCREENSAVER_STATUS property on root window #0.
* Element 0 is the status (0, BLANK, LOCK), element 1 is the time_t since
* the last state change, and subsequent elements are which hack is running
* on the various screens:
*   % xprop -f _SCREENSAVER_STATUS 32ac -root _SCREENSAVER_STATUS
*   _SCREENSAVER_STATUS(INTEGER) = BLANK, 1095196626, 10, 237
*
* See watch() in xscreensaver/driver/xscreensaver-command.c.
*
* @return The number of seconds the user has been idle.
*/

该文件包含为不同平台处理此问题的代码。

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:

/*
* Get the number of seconds the user has been idle.  In Unix-world
* this is based on the X Windows usage.  In MS Windows this is
* based on keyboard/mouse usage information obtained from the OS.
* In MacOS X, this is based on keyboard/mouse usage information
* obtained from the OS, if configure detected IOKit.  Otherwise,
* MacOS X is handled as a case of X Windows.
*
* In Debian bug #271639, jwz says:
*
* Purple should simply ask xscreensaver how long the user has been idle:
*   % xscreensaver-command -time
*   XScreenSaver 4.18: screen blanked since Tue Sep 14 14:10:45 2004
*
* Or you can monitor the _SCREENSAVER_STATUS property on root window #0.
* Element 0 is the status (0, BLANK, LOCK), element 1 is the time_t since
* the last state change, and subsequent elements are which hack is running
* on the various screens:
*   % xprop -f _SCREENSAVER_STATUS 32ac -root _SCREENSAVER_STATUS
*   _SCREENSAVER_STATUS(INTEGER) = BLANK, 1095196626, 10, 237
*
* See watch() in xscreensaver/driver/xscreensaver-command.c.
*
* @return The number of seconds the user has been idle.
*/

That file contains the code to handle this for the different platforms.

忘年祭陌 2024-12-08 08:43:22

我不知道明确的答案,但我知道我要去哪里寻找。 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.

情痴 2024-12-08 08:43:22

捕获诸如按键或鼠标移动之类的系统事件不是任何一种语言的领域。这基本上是操作系统管理的内容,并且因为您需要某种机制来侦听系统范围的事件,所以您必须以一种或另一种方式依赖操作系统提供的 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.

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