RAWINPUT - 如何获取鼠标滚轮数据

时间:2012-02-12 00:24:34

标签: c++ winapi directx raw-input

我正在使用带有directx的rawinput ...我正在尝试使用鼠标滚轮时使用相机进行缩放...当我使用以下代码运行程序时,我从rawinput获取usbuttondata的数据去了当我向前推鼠标滚轮时...到120它失控...高达65000 ......我认为数据应该是1或-1或0 ... rawinput作为鼠标发送什么车轮数据?

代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,
                             WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
        case WM_CREATE:
            {
                RAWINPUTDEVICE Rid[2];
                // Keyboard
                Rid[0].usUsagePage = 1;
                Rid[0].usUsage = 6;
                Rid[0].dwFlags = 0;
                Rid[0].hwndTarget=Inst.Wnd.hWnd;

                // Mouse
                Rid[1].usUsagePage = 1;
                Rid[1].usUsage = 2;
                Rid[1].dwFlags = 0;
                Rid[1].hwndTarget=Inst.Wnd.hWnd;
                if (!RegisterRawInputDevices(Rid,2,sizeof(RAWINPUTDEVICE)))
                {
                    MessageBox(NULL, L"Failed to Register Input Devices!", L"ALERT", MB_OK);
                    exit(1);
                }
                return 0;
            }
        case WM_INPUT:
            {               
                // Determine how big the buffer should be
                UINT iBuffer;

                GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &iBuffer,
                    sizeof(RAWINPUTHEADER));
                LPBYTE lpb = new BYTE[iBuffer];
                if (lpb == NULL)
                {
                    return 0;
                } 

                UINT readSize = GetRawInputData( (HRAWINPUT)lParam, RID_INPUT, lpb, &iBuffer, sizeof(RAWINPUTHEADER) ) ;

                if( readSize != iBuffer )
                    puts( "ERROR:  GetRawInputData didn't return correct size!" ) ;
                RAWINPUT *raw = (RAWINPUT*) lpb;                

                if (raw->header.dwType== RIM_TYPEMOUSE)
                {
                    riProcessMouseMessage(&raw->data.mouse);
                }
                if (raw->header.dwType== RIM_TYPEKEYBOARD)
                {
                    //riProcessKeyboardMessage(&raw->data.keyboard);
                }               
            }
            return 0;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDM_FILE_NEW:
                {
                    // Create the game object
                    pGame = new CGame(dxMgr.getD3DDevice());

                    // Initialize the game object
                    if (!pGame->init(Inst.Wnd.hWnd))
                        return 0;
                    break;
                }
                case IDM_FILE_OPEN:
                    pGame->m_animCollection->LoadXFile("oxana.x", 0);
                    //objects.CreateNewObject(1, L"oxana.x", NULL);
                    break;

                case IDM_FILE_SAVE:

                    break;

                case IDM_FILE_SAVEAS:
                    break;

                case IDM_FILE_EXIT:
                    PostQuitMessage(WM_QUIT);
                    break;
            }
            return 0;

        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            return 0;

        default:
            return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return TRUE;
}

void riProcessMouseMessage( const RAWMOUSE* rmouse )
{
    if(pGame != NULL)
    {
        //MessageBox(NULL, L"Game Found", L"SUCCESS", MB_OK);
        if ( MOUSE_MOVE_RELATIVE == rmouse->usFlags )   
        {
            riMgr.mxr = &rmouse->lLastX;        
            riMgr.myr = &rmouse->lLastY;    
        }
        riMgr.mzr = (RI_MOUSE_WHEEL & rmouse->usButtonFlags) ? &rmouse->usButtonData : 0;
    }
}

2 个答案:

答案 0 :(得分:6)

我怀疑它与WM_MOUSEWHEEL相同:

高位字表示车轮旋转的距离,以WHEEL_DELTA的倍数或分度表示,即120.正值表示车轮向前旋转,远离用户;负值表示车轮向后旋转,朝向用户。 低位字表示各种虚拟键是否已关闭。

因此您需要提取高阶词。您需要注意正确处理负值。你可能没有,因为你得到的是大值。

如果需要,可以使用以下宏:GET_WHEEL_DELTA_WPARAM(wParam)

答案 1 :(得分:0)

在切换语句中添加以下内容

case WM_MOUSEWHEEL:
{  
    int delta = GET_WHEEL_DELTA_WPARAM(wparam);
    if(delta > 0)
    {
        //Mouse Wheel Up
    }
    else
    {
        //Mouse Wheel Down
    }

    return 0;
}