/// <summary> /// Low-level keyboard hook procedure. /// The system calls this function every time a new keyboard input event /// is about to be posted into a thread input queue. The keyboard input /// can come from the local keyboard driver or from calls to the /// keybd_event function. If the input comes from a call to keybd_event, /// the input was "injected". However, the WH_KEYBOARD_LL hook is not /// injected into another process. Instead, the context switches back /// to the process that installed the hook and it is called in its /// original context. Then the context switches back to the application /// that generated the event. /// </summary> /// <param name="nCode"> /// The hook code passed to the current hook procedure. /// When nCode equals HC_ACTION, it means that the wParam and lParam /// parameters contain information about a keyboard message. /// </param> /// <param name="wParam"> /// The parameter can be one of the following messages: /// WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. /// </param> /// <param name="lParam">Pointer to a KBDLLHOOKSTRUCT structure.</param> /// <returns></returns> /// <see cref="http://msdn.microsoft.com/en-us/library/ms644985.aspx"/> /// private bool SetGlobalLLKeyboardHook() { // Create an instance of HookProc. globalLLKeyboardHookCallback = new HookProc(this.LowLevelKeyboardProc); hGlobalLLKeyboardHook = WindowsAPI.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, globalLLKeyboardHookCallback, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); return(hGlobalLLKeyboardHook != IntPtr.Zero); }
/// <summary> /// Set global low-level mouse hook /// </summary> /// <returns></returns> private bool SetGlobalLLMouseHook() { // Create an instance of HookProc. globalLLMouseHookCallback = new HookProc(this.LowLevelMouseProc); hGlobalLLMouseHook = WindowsAPI.SetWindowsHookEx( HookType.WH_MOUSE_LL, // Must be LL for the global hook globalLLMouseHookCallback, // Get the handle of the current module Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), // The hook procedure is associated with all existing threads running // in the same desktop as the calling thread. 0); return(hGlobalLLMouseHook != IntPtr.Zero); }