//----------------------------------------------------- // フック開始/終了 //----------------------------------------------------- // キー入力とマウス入力をフック public void Hook() { IntPtr hMod = Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); UnhookWindowsHookEx(mouseHook); UnhookWindowsHookEx(keyHook); mouseHandler = new MouseHookHandler(OnMouseLLHook); keyHandler = new KeyHookHandler(OnKeyLLHook); mouseHook = SetWindowsHookEx(WH.MOUSE_LL, mouseHandler, hMod, 0); keyHook = SetWindowsHookEx(WH.KEYBOARD_LL, keyHandler, hMod, 0); if (mouseHook == IntPtr.Zero || keyHook == IntPtr.Zero) { UnhookWindowsHookEx(mouseHook); UnhookWindowsHookEx(keyHook); int errorCode = Marshal.GetLastWin32Error(); Console.WriteLine(new Win32Exception(errorCode)); } }
//----------------------------------------------------- // フック開始/終了 //----------------------------------------------------- // キー入力とマウス入力をフック public void Hook() { IntPtr hMod = Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); UnhookWindowsHookEx(mouseHook); UnhookWindowsHookEx(keyHook); mouseHandler = new MouseHookHandler(OnMouseLLHook); keyHandler = new KeyHookHandler(OnKeyLLHook); mouseHook = SetWindowsHookEx(WH.MOUSE_LL, mouseHandler, hMod, 0); keyHook = SetWindowsHookEx(WH.KEYBOARD_LL, keyHandler, hMod, 0); if (mouseHook == IntPtr.Zero || keyHook == IntPtr.Zero) { UnhookWindowsHookEx(mouseHook); UnhookWindowsHookEx(keyHook); // int errorCode = Marshal.GetLastWin32Error(); // Console.WriteLine(new Win32Exception(errorCode)); hooking = false; } else { hooking = true; } }
static extern IntPtr SetWindowsHookEx(WH idHook, KeyHookHandler lpfn, IntPtr module, uint dwThreadId);