总的来说,我对 C# 有点陌生,我正在开发一个小程序来在空闲时呈现积压的 CAD 内容。我正在使用此处的 MouseKeyboardActivityMonitor 库:https://globalmousekeyhook.codeplex.com/但是有一些主要问题,例如在我的程序启动时输入被卡住,并且 Hook 没有检测到用户输入。我正在使用 Windows 8.1 x64 并针对 .NET 4.0 编译 DLL 和我自己的可执行文件
注意:我使用的是项目的版本3
这是我的代码(只是对钩子(Hook)的一个小测试):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Windows.Forms;
namespace HookTest1
{
class Program
{
static MouseHookListener mouseListener;
static KeyboardHookListener keyListener;
static void Main(string[] args)
{
Activate();
while (true)
{
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
private static void OnUserActivity()
{
Console.Write("UserAct");
}
private static void Activate()
{
mouseListener = new MouseHookListener(new GlobalHooker());
mouseListener.Enabled = true;
mouseListener.MouseDown += OnMouseActivity;
mouseListener.MouseMove += OnMouseActivity;
mouseListener.MouseWheel += OnMouseActivity;
keyListener = new KeyboardHookListener(new GlobalHooker());
mouseListener.Enabled = true;
keyListener.KeyDown += OnKeyActivity;
}
private static void OnMouseActivity(Object sender, System.Windows.Forms.MouseEventArgs e)
{
OnUserActivity();
}
private static void OnKeyActivity(Object sender, System.Windows.Forms.KeyEventArgs e)
{
OnUserActivity();
}
}
}
感谢所有帮助!
最佳答案
您可能已经在某个地方添加了它,只是在您的示例中将其删除了......但是您是否调用过
Application.Run();
在 Main() 中?据我了解,您正试图在没有消息循环的情况下 Hook 窗口事件。 Application.Run 将使它运行。
关于c# - 使用 GlobalMouseKeyHook 从空闲到操作 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138795/