종료할 때 EndEventCapture() 호출
StartEventCapture() 호출하면 WM_INPUT 메시지가 발생함
GetRawInputData를 사용해서 키보드, 마우스 이벤트를 가져오면 됨.
void StartEventCapture() { RAWINPUTDEVICE rawInputDev[2]; ZeroMemory(rawInputDev, sizeof(RAWINPUTDEVICE)*2); // 키보드 RAWINPUTDEVICE 구조체 설정 rawInputDev[0].usUsagePage = 0x01; rawInputDev[0].usUsage = 0x06; rawInputDev[0].dwFlags = RIDEV_INPUTSINK; rawInputDev[0].hwndTarget = GetSafeHwnd(); //마우스 RAWINPUTDEVICE 구조체 설정 rawInputDev[1].usUsagePage = 0x01; rawInputDev[1].usUsage = 0x02; rawInputDev[1].dwFlags = RIDEV_INPUTSINK; rawInputDev[1].hwndTarget = GetSafeHwnd(); if( FALSE == RegisterRawInputDevices(rawInputDev, 2, sizeof(RAWINPUTDEVICE)) ) { CString str; str.Format(_T("RegisterRawInputDevices Error %d"), GetLastError()); MessageBox(str); } } void EndEventCapture() { RAWINPUTDEVICE rawInputDev[2]; ZeroMemory(rawInputDev, sizeof(RAWINPUTDEVICE)*2); // 키보드 RAWINPUTDEVICE 구조체 설정 rawInputDev[0].usUsagePage = 0x01; rawInputDev[0].usUsage = 0x06; rawInputDev[0].dwFlags = RIDEV_REMOVE;// <== 이부분이 틀림 rawInputDev[0].hwndTarget = GetSafeHwnd(); //마우스 RAWINPUTDEVICE 구조체 설정 rawInputDev[1].usUsagePage = 0x01; rawInputDev[1].usUsage = 0x02; rawInputDev[1].dwFlags = RIDEV_REMOVE;// <== 이부분이 틀림 rawInputDev[1].hwndTarget = GetSafeHwnd(); if( FALSE == RegisterRawInputDevices(rawInputDev, 2, sizeof(RAWINPUTDEVICE)) ) { CString str; str.Format(_T("RegisterRawInputDevices Error %d"), GetLastError()); MessageBox(str); } } LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다. if( message == WM_INPUT ) { UINT dwSize = 40; static BYTE lpb[40]; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)); RAWINPUT* raw = (RAWINPUT*)lpb; if( raw->header.dwType == RIM_TYPEMOUSE ) { int xPosRelative = raw->data.mouse.lLastX; int yPosRelative = raw->data.mouse.lLastY; if( raw->data.mouse.ulButtons == 0 ) // move { InvalidateRect(CRect(0,50,150,100)); } else if( raw->data.mouse.ulButtons == 1 /*|| m_bLbuttonDragging*/ ) // l-button down { } else if( raw->data.mouse.ulButtons == 2 ) // l-button up { } else if( raw->data.mouse.ulButtons == 4 ) // r-button down { } else if( raw->data.mouse.ulButtons == 8 ) // r-button down { } else { } } if( raw->header.dwType == RIM_TYPEKEYBOARD ) { CString str; str.Format( _T(" Kbd: make=%04x Flags:%04x Reserved:%04x ExtraInformation:%08x, msg=%04x VK=%04x \n"), raw->data.keyboard.MakeCode, raw->data.keyboard.Flags, raw->data.keyboard.Reserved, raw->data.keyboard.ExtraInformation, raw->data.keyboard.Message, raw->data.keyboard.VKey); MSG msg; memset( &msg, 0, sizeof(MSG) ); msg.hwnd = m_hWnd; msg.message = raw->data.keyboard.Message; msg.wParam = raw->data.keyboard.VKey; // KeyMessageProcessing( &msg ); OutputDebugString( str ); } } return CDialog::WindowProc(message, wParam, lParam); }