我发现WH_MOUSE
并不总是被调用。问题可能是我正在使用WH_MOUSE
并不是WH_MOUSE_LL
?
代码:
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;
};
区别在于调用回调时的行为。
如果您使用低级版本,则不会受到 lpfn 带来的限制,因为对钩子函数的调用方式是这样的。请阅读下文了解更多信息。
引用 MSDN 的 SetWindowsHookEx 文档:
lpfn
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
并来自 LowLevelKeyboardProc:
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)