HID Input

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

HID Input

 
0
  #1
Feb 14th, 2006
I wrote down a program code that recognizes the keyboard as an HID device and gets input (the key pressed) from it. Then displays it on an edit control.

Here is the code :
  1. #include "stdafx.h"
  2. #define _WIN32_WINNT 0x0502
  3. #include <windows.h>
  4. #include "HID Input.h"
  5. #define MAX_LOADSTRING 100
  6.  
  7. RAWINPUTDEVICE Rid;
  8. USHORT usVKey;
  9. HWND hEdit;
  10.  
  11. LONG nWidth=200, nHeight=200;
  12. HWND hWnd;
  13. MSG Msg;
  14. LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
  15.  
  16. VOID RegisterInput(VOID);
  17. BOOL GetHIDData(LPARAM);
  18. VOID DisplayInput(VOID);
  19.  
  20. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
  21. {
  22. char szClassName[] = "WindowsApp";
  23.  
  24. WNDCLASSEX WindowClass;
  25. WindowClass.cbSize = sizeof (WNDCLASSEX);
  26. WindowClass.style = CS_DBLCLKS;
  27. WindowClass.lpfnWndProc = WindowProcedure;
  28. WindowClass.cbClsExtra = 0;
  29. WindowClass.cbWndExtra = 0;
  30. WindowClass.hInstance = hInstance;
  31. WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32. WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
  33. WindowClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  34. WindowClass.lpszMenuName = NULL;
  35. WindowClass.lpszClassName = szClassName;
  36. WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  37.  
  38. if (!RegisterClassEx(&WindowClass)) return 0;
  39.  
  40. hWnd = CreateWindowEx( (DWORD) 0,
  41. (LPCTSTR) szClassName,
  42. (LPCTSTR) "Application",
  43. (DWORD) WS_MINIMIZEBOX | WS_SYSMENU,
  44. (int) CW_USEDEFAULT,
  45. (int) CW_USEDEFAULT,
  46. (int) nWidth,
  47. (int) nHeight,
  48. (HWND) HWND_DESKTOP,
  49. (HMENU) NULL,
  50. (HINSTANCE) hInstance,
  51. (LPVOID) NULL);
  52.  
  53. ShowWindow(hWnd, nFunsterStil);
  54.  
  55. CreateWindowEx( (DWORD) NULL,
  56. (LPCTSTR) "STATIC",
  57. (LPCTSTR) "VKey of keyboard input :",
  58. (DWORD) WS_VISIBLE | WS_CHILD,
  59. (int) 5,
  60. (int) 5,
  61. (int) 185,
  62. (int) 20,
  63. (HWND) hWnd,
  64. (HMENU) NULL,
  65. (HINSTANCE) hInstance,
  66. (LPVOID) NULL);
  67.  
  68. hEdit = CreateWindowEx( (DWORD) NULL,
  69. (LPCTSTR) "EDIT",
  70. (LPCTSTR) NULL,
  71. (DWORD) WS_VISIBLE | WS_CHILD,
  72. (int) 5,
  73. (int) 35,
  74. (int) 50,
  75. (int) 20,
  76. (HWND) hWnd,
  77. (HMENU) NULL,
  78. (HINSTANCE) hInstance,
  79. (LPVOID) NULL);
  80.  
  81. RegisterInput();
  82.  
  83. while (GetMessage(&Msg, NULL, 0, 0))
  84. {
  85. TranslateMessage(&Msg);
  86. DispatchMessage(&Msg);
  87. }
  88. return (INT) Msg.wParam;
  89. }
  90.  
  91. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  92. {
  93. switch (message)
  94. {
  95. case WM_INPUT:
  96. if (GetHIDData(lParam) == FALSE) ErrorTest("Error gethering data.");
  97. DisplayInput();
  98. break;
  99. case WM_DESTROY:
  100. PostQuitMessage(0);
  101. break;
  102. default:
  103. return DefWindowProc (hwnd, message, wParam, lParam);
  104. }
  105.  
  106. return 0;
  107. }
  108.  
  109. BOOL GetHIDData(LPARAM hRawInput)
  110. {
  111. UINT cbSize;
  112. GetRawInputData((HRAWINPUT) hRawInput,
  113. (UINT) RID_INPUT,
  114. (LPVOID) NULL,
  115. (PUINT) &cbSize,
  116. (UINT) sizeof(RAWINPUTHEADER));
  117. LPBYTE lpbBuffer = new BYTE[cbSize];
  118.  
  119. GetRawInputData((HRAWINPUT) hRawInput,
  120. (UINT) RID_INPUT,
  121. (LPVOID) lpbBuffer,
  122. (PUINT) &cbSize,
  123. (UINT) sizeof(RAWINPUTHEADER));
  124.  
  125. RAWINPUT * pRawInput = (RAWINPUT *) lpbBuffer;
  126. if (pRawInput->header.dwType == RIM_TYPEKEYBOARD)
  127. {
  128. usVKey = pRawInput->data.keyboard.VKey;
  129. return TRUE;
  130. }
  131. else
  132. {
  133. return FALSE;
  134. }
  135. }
  136. VOID RegisterInput(VOID)
  137. {
  138. Rid.usUsagePage = 0x01;
  139. Rid.usUsage = 0x06;
  140. Rid.dwFlags = 0;
  141. if (RegisterRawInputDevices(&Rid, 1, sizeof(Rid)) == FALSE) ErrorTest("Error registering input");
  142. }
  143. VOID DisplayInput(VOID)
  144. {
  145. CHAR szEditText[4];
  146. _itoa((INT) usVKey, szEditText, 10);
  147. SetWindowText(hEdit, szEditText);
  148. }

The problem is, it doesn't show the virtual key code when the main program window is not active.

How can I make it work even if it is on the background or minimized?

I also attached the Visual Studio project files.
Attached Files
File Type: zip HID Input.zip (30.0 KB, 80 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: HID Input

 
0
  #2
Feb 14th, 2006
Modify your RegisterInput function as follows.
  1. VOID RegisterInput(HWND hWnd;)
  2. {
  3. Rid.usUsagePage = 0x01;
  4. Rid.usUsage = 0x06;
  5. Rid.dwFlags = RIDEV_INPUTSINK;
  6. Rid.hwndTarget = hWnd; // The Window Handle of your window
  7. if (RegisterRawInputDevices(&Rid, 1, sizeof(Rid)) == FALSE) ErrorTest("Error registering input");
  8. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

Re: HID Input

 
0
  #3
Feb 14th, 2006
Yes, thanks.
It works now.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC