4

我正在从键盘和鼠标中获取原始数据——键盘数据正在完美地接收到我的应用程序中,我完全没有遇到任何问题。不知何故,我的应用程序没有收到任何鼠标数据。事实上,它甚至没有通过该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;
              }

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

4

1 回答 1

4

这是将标志添加RIDEV_INPUTSINKdwFlags. 我的应用程序不在前台 - 并且此标志使您能够接收系统范围的输入(您的应用程序无需处于焦点即可接收输入)。

于 2012-06-18T18:30:18.047 回答