如何检测在Windows上运行的Java服务用户的活动?用户、Windows、Java

2023-09-12 05:11:35 作者:柠檬校草。

我的目标是使用Java系统监测中的应用。我想知道,当用户在做的活动在Windows PC上。其结果将是这样的:

My goal is to create a system monitoring application using Java. I would like to know when a user is doing activity on a Windows PC. The result would be something like this:

8:00 - 8:15活动

8:00 - 8:15 activity

9:12 - 10:29活动

9:12 - 10:29 activity

12:24 - 15:34活动

12:24 - 15:34 activity

我不感兴趣的任何其他信息(其中重点是pressed,使用的应用程序等)。只有用户的活动。

I'm not interested in any other information (which key was pressed, application used, etc.). Only user activity.

在Java中这甚至可能吗?我打算运行我的Java应用程序作为服务。但对于越来越事件当用户使用计算机时,我不知道从哪里开始。

Is this even possible in Java? I plan to run my java application as a service. But as for getting events when a user uses the computer, I have no idea where to start.

进一步澄清:我没有兴趣在详细信息的活动中,只有一个用户移动鼠标或pressed的关键。我不在乎哪个键是pressed,只要我知道的一个关键是pssed在应用程序中的某个地方$ P $。我也不在乎,除非关键pressed和鼠标运动的任何其他活动(比如,我不感兴趣,如果一个USB密钥插入USB端口)。

Further clarifications: I'm not interested in the details of the activity, only that a user has moved the mouse or pressed a key. I don't care which key was pressed, as long as I know that a key was pressed in an application somewhere. I also don't care for any other activity except key pressed and mouse movement (for example, I am not interested if a USB key is inserted in a USB port).

推荐答案

您不能监视直接从服务用户的活动。这项服务将运行在从用户的活动不同的窗口站,因此将无法挂接到该活动(除通过将需要用C语言编写的筛选器驱动程序)。

You cannot monitor user activity directly from a service. The service will be running in a different window station from the users activities and so will have no way to hook into that activity (except through filter drivers that would need to be written in C).

所以,你需要一个运行在用户的桌面和挂接到键盘和鼠标活动的客户端应用程序。你会做到这一点通过两个调用Windows API SetWindowsHookEx函数使用JNI(低级别的键盘和鼠标钩子)。要监视活动的应用程序然后将需要处理的键盘和鼠标钩子消息。

So you will need a client application that runs in the user's desktop and hooks into the keyboard and mouse activity. You would do that via two calls to the Windows API SetWindowsHookEx (for low level keyboard and mouse hooks) using JNI. To monitor activity the application would then need to process the keyboard and mouse hooks for messages.

您可以通过添加一个条目添加到注册表的Run键启动应用程序自动启动,或者你可以让你的服务监视器有关事件的会话日志和launch该应用程序它。然后用户会话应用可以或者处理信息本身或它经由管道或插座传递给服务。

You could launch the application as auto-start by adding an entry to the registry's Run key or you could have your service monitor for session log on events and launch the application from it. Then the user session application could either process the information itself or pass it to the service via a pipe or socket.