捕获原始输入设备信息。记录键盘和鼠标操作。//先注册原始输入设备.RAWINPUTDEVICE raw;
raw.hwndTarget = AfxGetMainWnd()->m_hWnd;
raw.usUsagePage = 0x01;
raw.usUsage = 0x02;  //鼠标 raw.usUsage = 0x06;   //键盘
raw.dwFlags = RIDEV_INPUTSINK;
if(!RegisterRawInputDevices(&raw, 1, sizeof(RAWINPUTDEVICE)))
MessageBox(L"注册键盘设备失败",L"注册原始输入设备");
//用类向导在程序里添加消息响应函数void CmagickeyDlg::OnRawInput(UINT nInputcode, HRAWINPUT hRawInput)
{
        if (RIM_INPUT == nInputcode)   //这句是什么意思
{
return CDialogEx::OnRawInput(nInputcode, hRawInput);
}
        
        //下面获取输入数据
LPBYTE lpb = nullptr;
UINT uint_size;
GetRawInputData(hRawInput, RID_INPUT, nullptr, &uint_size, sizeof(RAWINPUTHEADER));
lpb = new BYTE[uint_size];
if(nullptr == lpb)
{
MessageBox(L"没有分配内存", L"Raw Input Test");
delete[] lpb;
return CDialogEx::OnRawInput(nInputcode, hRawInput);
}
if(GetRawInputData(hRawInput, RID_INPUT, lpb, &uint_size, sizeof(RAWINPUTHEADER)) != uint_size)
{
MessageBox(L"GetRawInputDate dosen't return correct size!", L"Raw Input Test");
delete[] lpb;
return CDialogEx::OnRawInput(nInputcode, hRawInput);
} RAWINPUT raw = *((RAWINPUT*)lpb); delete[] lpb;  //先delete,怕以后忘了//下面是可以对捕获的数据进行操作
}
现在这个只能捕获键盘或者鼠标,不能同时捕获键盘和鼠标。
该怎么设置才能同时捕获键盘和鼠标?

解决方案 »

  1.   

    VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
      

  2.   


    RAWINPUTDEVICE raw[2];
        raw[0].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[0].usUsagePage = 0x01;
        raw[0].usUsage = 0x02;  //鼠标
        raw[0].dwFlags = RIDEV_INPUTSINK;
        raw[1].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[1].usUsagePage = 0x01;
        raw[1].usUsage = 0x06;   //键盘
        raw[1].dwFlags = RIDEV_INPUTSINK;
        if(!RegisterRawInputDevices(&raw[0], 2, 2*sizeof(RAWINPUTDEVICE)))
            MessageBox(L"注册键盘设备失败",L"注册原始输入设备");
    试试看,不一定对。
      

  3.   

    可以,谢谢zhao4zhong1。
      

  4.   


    RAWINPUTDEVICE raw[2];
        raw[0].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[0].usUsagePage = 0x01;
        raw[0].usUsage = 0x02;  //鼠标
        raw[0].dwFlags = RIDEV_INPUTSINK;
        raw[1].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[1].usUsagePage = 0x01;
        raw[1].usUsage = 0x06;   //键盘
        raw[1].dwFlags = RIDEV_INPUTSINK;
        if(!RegisterRawInputDevices(&raw[0], 2, 2*sizeof(RAWINPUTDEVICE)))  //不能数组注册,只能分别注册
            MessageBox(L"注册键盘设备失败",L"注册原始输入设备");
      

  5.   

    Devices不是Device的复数形式吗?RegisterRawInputDevices Function--------------------------------------------------------------------------------The RegisterRawInputDevices function registers the devices that supply the raw input data.SyntaxBOOL RegisterRawInputDevices(          PCRAWINPUTDEVICE pRawInputDevices,
        UINT uiNumDevices,
        UINT cbSize
    );
    ParameterspRawInputDevices
    [in] Pointer to an array of RAWINPUTDEVICE structures that represent the devices that supply the raw input.
    uiNumDevices
    [in] Number of RAWINPUTDEVICE structures pointed to by pRawInputDevices.
    cbSize
    [in] Size, in bytes, of a RAWINPUTDEVICE structure.
    Return ValueTRUE if the function succeeds; otherwise, FALSE. If the function fails, call GetLastError for more information.
    ResTo receive WM_INPUT messages, an application must first register the raw input devices using RegisterRawInputDevices. By default, an application does not receive raw input.Function InformationMinimum DLL Version user32.dll 
    Header Declared in Winuser.h, include Windows.h 
    Import library User32.lib 
    Minimum operating systems Windows XP See AlsoRaw Input, RAWINPUTDEVICE, WM_INPUT--------------------------------------------------------------------------------
      

  6.   

    有可能得
    if(!RegisterRawInputDevices(&raw[0], 2, sizeof(RAWINPUTDEVICE)))
      

  7.   

    可以,修改下。谢谢赵大神。    RAWINPUTDEVICE raw[2];
        raw[0].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[0].usUsagePage = 0x01;
        raw[0].usUsage = 0x02;  //鼠标
        raw[0].dwFlags = RIDEV_INPUTSINK;
        raw[1].hwndTarget = AfxGetMainWnd()->m_hWnd;
        raw[1].usUsagePage = 0x01;
        raw[1].usUsage = 0x06;   //键盘
        raw[1].dwFlags = RIDEV_INPUTSINK;
        if(!RegisterRawInputDevices(&raw[0], 2, sizeof(RAWINPUTDEVICE)))
            MessageBox(L"注册键盘设备失败");这样就可以用了。