获取鼠标滚轮移动数据

时间:2014-06-09 12:57:51

标签: c++ windows winapi

我写了一个窗口挂钩来检索鼠标事件

_handle = SetWindowsHookEx(WH_MOUSE,
                    (HOOKPROC)KeyEvent,
                    nullptr,
                    GetCurrentThreadId());

static LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode >= 0)
    {
         MOUSEHOOKSTRUCT* mStruct = ( MOUSEHOOKSTRUCT*)lParam;
         MSLLHOOKSTRUCT* mWheelDStruct = (MSLLHOOKSTRUCT*)lParam;

         CMouseHookCom::_this->ReportEventW(mStruct->hwnd, wParam, mWheelDStruct);
    }

    return(CallNextHookEx(NULL, nCode, wParam, lParam));
}

case WM_MOUSEWHEEL:
        OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL");
        strm = (MSLLHOOKSTRUCT*)extraData;
        zDelta = (short)(HIWORD(((MSLLHOOKSTRUCT*)extraData)->mouseData));
        _stprintf(buffer, L"CMouseHookCom - WM_MOUSEWHEEL delta %d %i", zDelta, short((strm->mouseData >> 16) & 0xffff));
        OutputDebugString(buffer);
        if (zDelta > 0)
        {
            OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL UP");
            Invoke_onScrollOut(componentId);
        }
        else
        {
            OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL DOWN");
            Invoke_onScrollIn(componentId);
        }
        break;
    default:
        _stprintf(buffer, L"CMouseHookCom - UnHandleled event %d", event);
        OutputDebugString(buffer);
        break;
    }

无论我如何尝试,我总能获得轮子的积极成果。我总是得到不同的价值。检索数据的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您应该使用Raw Input而不是鼠标钩子。这样可以减少操作系统监控鼠标的开销,它可以为您提供钩子不会的信息。