gpt4 book ai didi

c++ - WH_MOUSE 和 WH_MOUSE_LL 钩子(Hook)之间有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:23 26 4
gpt4 key购买 nike

我发现 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;
};

最佳答案

不同之处在于调用回调时的行为。如果您使用的是低级版本,则不会因为调用 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.

关于c++ - WH_MOUSE 和 WH_MOUSE_LL 钩子(Hook)之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/872677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com