What are all the differences between WH_MOUSE and WH_MOUSE_LL hooks?

I found that WH_MOUSE not always called. Could the problem be that I'm using WH_MOUSE and not WH_MOUSE_LL ?

Code:

 class MouseHook { public: static signal<void(UINT, const MOUSEHOOKSTRUCT&)> clickEvent; static bool install() { if (isInstalled()) return true; hook = ::SetWindowsHookEx(WH_MOUSE, (HOOKPROC)&mouseProc, ::GetModuleHandle(NULL), NULL); return(hook != NULL); } static bool uninstall() { if (hook == NULL) return TRUE; bool fOk = ::UnhookWindowsHookEx(hook); hook = NULL; return fOk != FALSE; } static bool isInstalled() { return hook != NULL; } private: static LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION && (wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN || wParam == WM_RBUTTONDOWN || wParam == WM_NCRBUTTONDOWN || wParam == WM_MBUTTONDOWN || wParam == WM_NCMBUTTONDOWN )) { MOUSEHOOKSTRUCT* mhs = (MOUSEHOOKSTRUCT*) lParam; clickEvent(wParam, *mhs); } return ::CallNextHookEx(hook, nCode, wParam, lParam); } static HHOOK hook; }; 
+4
source share
1 answer

The difference is the behavior caused by the callback. If you use the low-level version, you do not bear the restrictions set by lpfn due to the way your hook function is called. Please read below for more information. Quote from the MSDN document for SetWindowsHookEx:

lpfn [in] Pointer to a hook procedure. If the dwThreadId parameter is zero or indicates the identifier of a thread created by another process, the lpfn parameter must point to a hook procedure in the DLL. Otherwise, lpfn may point to a hook procedure in the code associated with the current process.

and from LowLevelKeyboardProc:

hook WH_KEYBOARD_LL is not being entered into another process. Instead, the context switches back to the process that set the hook, and it is called in its original context. Then the context switches back to the application that generates the event.
+10
source

All Articles