public void InitHook() { uint id = GetCurrentThreadId(); MouseProcDelegate += MouseProc; mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProcDelegate, IntPtr.Zero, id); if (mouseHook.ToInt32() == 0) { MessageBox.Show("mouse hook failed"); } KeyboardProcDelegate += KeyboardProc; using (System.Diagnostics.Process curProcess = System.Diagnostics.Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProcDelegate, GetModuleHandle(curModule.ModuleName), 0); if (keyboardHook.ToInt32() == 0) { MessageBox.Show("keboard hook failed"); } } }
static extern IntPtr SetWindowsHookEx(int code, HookProcHandle func, IntPtr hInstance, uint threadID);