Search code examples
c++winapimouseraw-input

Raw Input mouse - No data received


I'm getting raw data from both the keyboard and the mouse - the keyboard data is being received to my application perfectly, I'm having no trouble at all. Somehow, none of the mouse data is being received to my application. In fact, it doesn't even pass the if (raw->header.dwType == RIM_TYPEMOUSE) clause.

case WM_CREATE:{

    /*  ...
        other initialisations 
        ... */

    // register for raw data
    rid[0].dwFlags=RIDEV_NOLEGACY|RIDEV_INPUTSINK;  // ignore legacy messages
    rid[0].usUsagePage=1;                           
    rid[0].usUsage=6;                               // keyboard
    rid[0].hwndTarget=hWnd;

    rid[1].dwFlags=RIDEV_NOLEGACY;                  // ignore legacy messages   
    rid[1].usUsagePage=1;                           
    rid[1].usUsage=2;                               // mouse
    rid[1].hwndTarget=hWnd;



    RegisterRawInputDevices(rid,2,sizeof(RAWINPUTDEVICE));

    break;
}

case WM_INPUT:{         
    if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER))==-1){
        break;
    }
    LPBYTE lpb=new BYTE[dwSize];
    if(lpb==NULL){
        break;
    } 
    if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb,&dwSize,sizeof(RAWINPUTHEADER))!=dwSize){
        delete[] lpb;
        break;
    }

    PRAWINPUT raw=(PRAWINPUT)lpb;
    UINT KeyEvent;

    if (raw->header.dwType == RIM_TYPEKEYBOARD)
    {
        KeyEvent=raw->data.keyboard.Message;
        keyChar=MapVirtualKey(raw->data.keyboard.VKey,MAPVK_VK_TO_CHAR);

        if(KeyEvent==WM_KEYDOWN){

            /* Works great */

        }

    }

    else if (raw->header.dwType == RIM_TYPEMOUSE)
    {

        WriteFile(hFile, TEXT_ENTER, strlen(TEXT_ENTER), &fWritten, 0);                 // TEXT_ENTER contains "Mouse event occured"
            // Even this never gets executed. My file never gets logged with this message

        LMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN;
        MMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN;
        RMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN;


        if (LMButtonDown)
            WriteFile(hFile, LCLICK, strlen(LCLICK), &fWritten, 0);                 // LCLICK contains "Left mouse button clicked"
        if (MMButtonDown)
            WriteFile(hFile, MCLICK, strlen(MCLICK), &fWritten, 0);                 // MCLICK contains "Middle mouse button clicked"
        if (RMButtonDown)
            WriteFile(hFile, RCLICK, strlen(RCLICK), &fWritten, 0);                 // RCLICK contains "Right mouse button clicked" */
    }
    delete[] lpb;       
    CloseHandle(hFile);
    break;
              }

Can anybody spot any mistake, and tell me what it is?


Solution

  • It was a simple case of adding the flag RIDEV_INPUTSINK to dwFlags. My application isn't in the foreground - and this flag enables you to receive system-wide input (your application need not be in focus to receive the input).