gpt4 book ai didi

c# - 全局鼠标事件处理程序

转载 作者:IT王子 更新时间:2023-10-29 04:09:36 26 4
gpt4 key购买 nike

我有以下从某处获取的代码,用于捕获鼠标事件。我修改了它并制作了一个事件处理程序,以便我可以订阅它。鼠标事件被正确捕获。但它从不触发事件处理程序。任何人都可以找出代码有什么问题吗?

public static class MouseHook
{
public static event EventHandler MouseAction = delegate { };

public static void Start() => _hookID = SetHook(_proc);
public static void stop() => UnhookWindowsHookEx(_hookID);

private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}

private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
MouseAction(null,new EventArgs());
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

private const int WH_MOUSE_LL = 14;

private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}

[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData, flags, time;
public IntPtr dwExtraInfo;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}

我是这样订阅的

MouseHook.Start();  
MouseHook.MouseAction += new EventHandler(Event);

接收事件的函数。

private void Event(object sender, EventArgs e) => Console.WriteLine("Left mouse click!"); 

更新:我将工作代码放在一起 open source nuget package for user action hooks.

最佳答案

return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);

当您在早于 Windows 8 的 Windows 版本上的 .NET 4 上运行此代码时,此代码将失败。CLR 不再模拟托管程序集的非托管模块句柄。您无法在代码中检测到此故障,因为它缺少所需的错误检查。在 GetModuleHandle 和 SetWindowsHookEx 上。 pinvoke 时切勿跳过错误检查,winapi 不会抛出异常。检查它们是否返回 IntPtr.Zero 并在返回时简单地抛出 Win32Exception。

修复很简单,SetWindowsHookEx() 需要一个有效的模块句柄,但在设置低级鼠标 Hook 时实际上并不使用它。所以任何句柄都可以,您可以传递 user32.dll 的句柄,它始终加载在 .NET 应用程序中。修复:

IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle("user32"), 0);
if (hook == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}
return hook;

关于c# - 全局鼠标事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com