gpt4 book ai didi

c++ - 在鼠标 Hook 中,滚轮增量始终为 0

转载 作者:行者123 更新时间:2023-12-02 02:02:31 25 4
gpt4 key购买 nike

#include <iostream>
#include <windows.h>

LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam){
if(wParam == WM_MOUSEWHEEL){
std::cout << "wheel: " << GET_WHEEL_DELTA_WPARAM(wParam) << std::endl;
}else{
MOUSEHOOKSTRUCT* mouselparam = (MOUSEHOOKSTRUCT*)lParam;
std::cout << "etc: " << wParam << " - " << mouselparam->pt.x << "x" << mouselparam->pt.y << std::endl;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main(int argc, char *argv[]) {

HHOOK hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, 0, 0);

MSG msg;
while (!GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

UnhookWindowsHookEx(hhkLowLevelMouse);

return 0;
}

这是完整的代码。

“etc:”完全按照我的预期工作。

“wheel:”始终输出 0。

我错过了什么吗?

使用 HIWORD 而不是 GET_WHEEL_DELTA_WPARAM 会得到相同的结果。

最佳答案

GET_WHEEL_DELTA_WPARAM() 仅适用于真实 WM_MOUSEWHEEL 窗口消息的 wParam,而不是一个 WH_MOUSE_LL 钩子(Hook)。

在钩子(Hook)中,wParam本身只是消息标识符,仅此而已。所有鼠标详细信息都存储在 lParam 指向的 MSLLHOOKSTRUCT 结构中。您试图查看除 WM_MOUSEWHEEL 之外的所有鼠标消息,但您查看的是错误的结构(MOUSEHOOKSTRUCT 使用>WH_MOUSE,而不是WH_MOUSE_LL)。

根据 LowLevelMouseProc callback function文档:

  • wParam [in]
    Type: WPARAM

    The identifier of the mouse message. This parameter can be one of the following messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_RBUTTONDOWN, or WM_RBUTTONUP.

  • lParam [in]
    Type: LPARAM

    A pointer to an MSLLHOOKSTRUCT structure.

还有MSLLHOOKSTRUCT structure文档:

mouseData
Type: DWORD

If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. The low-order word is reserved. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.

试试这个:

LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam){
if (nCode == HC_ACTION) {
MSLLHOOKSTRUCT* mouselparam = (MSLLHOOKSTRUCT*)lParam;
if (wParam == WM_MOUSEWHEEL) {
short delta = HIWORD(mouselparam->mouseData);
// alternatively, GET_WHEEL_DELTA_WPARAM() would also work here:
// short delta = GET_WHEEL_DELTA_WPARAM(mouselparam->mouseData);
std::cout << "wheel: " << delta << std::endl;
}else{
std::cout << "etc: " << wParam << " - " << mouselparam->pt.x << "x" << mouselparam->pt.y << std::endl;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

关于c++ - 在鼠标 Hook 中,滚轮增量始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68827267/

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