internal static void SetMouseHook(NativeWinApi.HookProc proc) { SetHook(NativeWinApi.HookType.WH_MOUSE_LL, proc); }
private static IntPtr SetHook(NativeWinApi.HookType hookType, NativeWinApi.HookProc proc) { using (var curProcess = Process.GetCurrentProcess()) using (var curModule = curProcess.MainModule) { var registeredHook = NativeWinApi.SetWindowsHookEx(hookType, proc, NativeWinApi.GetModuleHandle(curModule.ModuleName), 0); if (registeredHook == IntPtr.Zero) { throw new Exception("Failed to register hook for hooktype " + hookType); } return registeredHook; } }
// http://stackoverflow.com/questions/10280000/how-to-create-lparam-of-sendmessage-wm-keydown private void SendKeyCode(IntPtr hwnd, NativeWinApi.VirtualKeys keyCode, bool extended, NativeWinApi.Messages msgs) { var scanCode = NativeWinApi.MapVirtualKey((uint)keyCode, 0); uint lParam; lParam = (0x00000001 | (scanCode << 16)); if (extended) { lParam = lParam | 0x01000000; } NativeWinApi.SendMessage(hwnd, msgs, (IntPtr)keyCode, (IntPtr)lParam); }