P. Avery
P. Avery

Reputation: 809

RAWINPUT - How to get Mouse Wheel data

I'm using rawinput with directx...i'm trying to zoom with the camera when mouse wheel is used...when I run the program with the following code, the data I get from rawinput for the usbuttondata goes to 120 when I push mouse wheel forward...then it goes out of control...up to 65000...I thought the data was supposed to be 1 or -1 or 0...what does rawinput send as the mouse wheel data?

code:

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;
    }
}

Upvotes: 3

Views: 6477

Answers (3)

bobobobo
bobobobo

Reputation: 67224

The WM_INPUT is mousewheel if you have raw->header.dwType== RIM_TYPEMOUSE and also

if( raw->data.mouse.ulButtons & RI_MOUSE_WHEEL ) {
  short wheel = (short)raw->data.mouse.usButtonData; // signed value
  // - is scroll down, + is scroll up
  float wheelSteps = (float)wheel / WHEEL_DELTA;
}

Upvotes: 1

user10211028
user10211028

Reputation: 11

Add the following in the switch statment

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

    return 0;
}

Upvotes: 0

rasmus
rasmus

Reputation: 3216

I suspect it is the same as WM_MOUSEWHEEL:

The high-order word indicates the distance the wheel is rotated, expressed in multiples or divisions of WHEEL_DELTA, which is 120. 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. The low-order word indicates whether various virtual keys are down.

Therefore you need to extract the high order word. You need to take care to handle negative values correctly. You probably don't as you get large values instead.

If you want you can use the following macro for this: GET_WHEEL_DELTA_WPARAM(wParam)

Upvotes: 8

Related Questions