我正在尝试设置一个低级别的 Windows 键盘挂钩来抓取三个按下的键,即使应用程序不在焦点上也是如此。为此,我将 SetWindowsHookEx 称为
// Create an instance of HookProc.
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//install hook
hKeyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
KeyboardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
//If SetWindowsHookEx fails.
if (hKeyboardHook == 0)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(false, true, false);
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);
}
这曾经在使用 .NET Framework 3.5 的 32 位和 64 位机器上工作,但在升级到 .NET Framework 4.0 后停止在 32 位机器上工作。
有谁知道如何解决这个问题,以便我可以使用 4.0 框架并使其在 32 位和 64 位机器上工作?