原始输入鼠标 - 未收到数据

时间:2012-06-16 20:51:35

标签: c++ winapi mouse raw-input

我从键盘和鼠标中获取原始数据 - 键盘数据正在接收到我的应用程序,我没有遇到任何麻烦。不知何故,我的应用程序没有收到任何鼠标数据。实际上,它甚至没有通过if (raw->header.dwType == RIM_TYPEMOUSE)子句。

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

任何人都可以发现任何错误,并告诉我它是什么?

1 个答案:

答案 0 :(得分:4)

将标志RIDEV_INPUTSINK添加到dwFlags这是一个简单的案例。我的应用程序不在前台 - 这个标志使您可以接收系统范围的输入(您的应用程序无需关注以接收输入)。