捕獲原始輸入設備資訊。記錄鍵盤和滑鼠操作。
//先注冊原始輸入設備.
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,怕以后忘了
//下面是可以對捕獲的資料進行操作
}
現在這個只能捕獲鍵盤或者滑鼠,不能同時捕獲鍵盤和滑鼠。
該怎么設定才能同時捕獲鍵盤和滑鼠?
uj5u.com熱心網友回復:
VS IDE中,在不明白的符號上點滑鼠右鍵,選轉到定義。uj5u.com熱心網友回復:
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"注冊原始輸入設備");
試試看,不一定對。
uj5u.com熱心網友回復:
可以,謝謝zhao4zhong1。uj5u.com熱心網友回復:
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"注冊原始輸入設備");
uj5u.com熱心網友回復:
這算是“瞎貓碰上死耗子”嗎?


uj5u.com熱心網友回復:
Devices不是Device的復數形式嗎?RegisterRawInputDevices Function
--------------------------------------------------------------------------------
The RegisterRawInputDevices function registers the devices that supply the raw input data.
Syntax
BOOL RegisterRawInputDevices( PCRAWINPUTDEVICE pRawInputDevices,
UINT uiNumDevices,
UINT cbSize
);
Parameters
pRawInputDevices
[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 Value
TRUE if the function succeeds; otherwise, FALSE. If the function fails, call GetLastError for more information.
Remarks
To 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 Information
Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows XP
See Also
Raw Input, RAWINPUTDEVICE, WM_INPUT
--------------------------------------------------------------------------------
uj5u.com熱心網友回復:
有可能得if(!RegisterRawInputDevices(&raw[0], 2, sizeof(RAWINPUTDEVICE)))
uj5u.com熱心網友回復:
可以,修改下。謝謝趙大神。
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"注冊鍵盤設備失敗");
這樣就可以用了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/132533.html
標籤:基礎類
上一篇:MFC記事本資料傳輸出現中文例外
下一篇:debug下編譯通過,但是release下編譯出錯LINK : fatal error LNK1104: cannot open file "ReleaseMi
