[DllImport("user32.dll")] private static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { // Handle the incoming message return CallNextHookEx(hookId, nCode, wParam, lParam); } // Set up a hook for mouse events var hookId = SetWindowsHookEx(WH_MOUSE_LL, HookCallback, IntPtr.Zero, 0);
[DllImport("user32.dll")] private static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { // Handle the incoming message return CallNextHookEx(hookId, nCode, wParam, lParam); } // Set up a hook for keyboard events var hookId = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, IntPtr.Zero, 0);This example is similar to the previous one, but we set up a hook for keyboard events instead of mouse events. We use the hook type for keyboard events and pass in the same HookCallback method as before. The package library for HookProc is part of the standard C# library and does not require any additional packages to be installed.