我的主要目标是记录用户在应用程序上花费的“总时间”。我想在 Windows 中创建一个将安装在客户端 PC 上的后台代理。该代理就像一个键盘记录器,位于后台并记录以下内容:
- 用户“启动”特定应用程序时的时间戳。
- 在应用程序上花费的空闲时间与活动时间。
- 用户“结束”特定应用程序的时间戳。
我的挑战是:
- 如何创建后台代理。
- 如何记录用户何时启动和结束应用程序。
任何帮助将不胜感激。
我用 C# 编写了一个 Keylogger API,它可以挂钩用户按下的任何键和鼠标单击。当其中一个事件发生时,API 会返回一个回调,其中包含一个对象,该对象说明按下了哪个键以及用户在哪个屏幕上。鼠标单击也是如此,它返回用户单击的位置以及那一刻的焦点所在的屏幕。
在我的 API 中,这是我用来获取焦点屏幕的方法,您可以根据自己的需要进行调整:
private static string CurrentWindowTitle()
{
int hwnd = GetForegroundWindow();
StringBuilder title = new StringBuilder(1024);
int textLength = GetWindowText(hwnd, title, title.Capacity);
if ((textLength <= 0) || (textLength > title.Length))
return "[Unknown]";
return $"[{title}]";
}
方法GetForegroundWindow()
和GetWindowText()
只是 User32.dll 的方法:
[DllImport("User32.dll")]
internal static extern int GetForegroundWindow();
[DllImport("User32.dll")]
internal static extern int GetWindowText(int hwnd, StringBuilder s, int nMaxCount);
我提到的 API 的源代码可以在这里找到