944,172 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5004
  • C RSS
Feb 28th, 2006
0

Using HID Input

Expand Post »
Hello guys. I'm capturing the keyboard input using the class below. And I have no problem doing that.
What I wanna ask is how to understand if a key is pressed, released or being pressed.

Let me explain more...
When ever any key is pressed the member function GetData() returns the virtual key code of the key which is pressed. But, if the user keeps pressing the key, the WM_INPUT message is sent periodically. The frequence of it is set at the control panel of Windows. And also, when the user releases the key, again the WM_INPUT is sent with the same virtual key code.

For example,
When user hits the escape key, 27 returns from GetData() twice. One for pressing, one for releasing.

And, when the 27 returns for the second time from GetData(), I want to find out if the key is still being pressed or the key is just released.

Waiting for your replies...

  1. class HIDInput
  2. {
  3. private:
  4. RAWINPUTDEVICE Rid;
  5. public:
  6. LPARAM hRawInput;
  7. HWND hTargetWnd;
  8. USHORT LastKey;
  9.  
  10. void Register(void)
  11. {
  12. Rid.usUsagePage = 0x01;
  13. Rid.usUsage = 0x06;
  14. Rid.dwFlags = RIDEV_INPUTSINK;
  15. Rid.hwndTarget = hTargetWnd;
  16. if (RegisterRawInputDevices(&Rid, 1, sizeof(Rid)) == FALSE) DebugTest("Error registering input");
  17. }
  18. unsigned int GetData(void)
  19. {
  20. UINT cbSize;
  21. GetRawInputData((HRAWINPUT) hRawInput,
  22. (UINT) RID_INPUT,
  23. (LPVOID) NULL,
  24. (PUINT) &cbSize,
  25. (UINT) sizeof(RAWINPUTHEADER));
  26. LPBYTE lpbBuffer = new BYTE[cbSize];
  27.  
  28. GetRawInputData((HRAWINPUT) hRawInput,
  29. (UINT) RID_INPUT,
  30. (LPVOID) lpbBuffer,
  31. (PUINT) &cbSize,
  32. (UINT) sizeof(RAWINPUTHEADER));
  33.  
  34. RAWINPUT * pRawInput = (RAWINPUT *) lpbBuffer;
  35. if (pRawInput->header.dwType == RIM_TYPEKEYBOARD)
  36. return LastKey = pRawInput->data.keyboard.VKey; //This one never returns 0
  37. else
  38. return 0; //Error
  39.  
  40. }
Similar Threads
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Feb 28th, 2006
0

Re: Using HID Input

use the
  1. pRawInput->data.keyboard.Message
field. It returns 0x101 when released and 0x100 ( i think it is 0x100, check the documentation just in case ) when it is pressed. You can use an if condition to check the two.

something like
  1. if ( pRawInput->data.keyboard.Message = 0x100 ) // Pressed
  2. {
  3. // Do processing
  4. }
  5. else
  6. {
  7. //Ignore
  8. }
As for the user keeping on pressing, Maybe checking for consecutive 0x100 values with the same VKey will do the trick.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 1st, 2006
0

Re: Using HID Input

Windows sends the key twice. First time it sends the key with a WM_KEYDOWN, and then with a WM_KEYUP message. As a special case, it sends WM_SYSKEYDOWN and WM_SYSKEYUP when ALT key is pressed. No problem so far.

But when either Windows, insert, delete, home, end, pageup or pagedown key is pressed, WM_KEYDOWN and WM_KEYUP messages are sent twice. I mean, 2x2=4 times.
So, what about this situation?
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Mar 1st, 2006
0

Re: Using HID Input

Quote ...
But when either Windows, insert, delete, home, end, pageup or pagedown key is pressed, WM_KEYDOWN and WM_KEYUP messages are sent twice. I mean, 2x2=4 times.
So, what about this situation?
All these Keys are Extended Keys. If you read the documentation for WM_KEYDOWN, WM_KEYUP ..., you will see that the 25th bit of the lParam value of the respective message tells you if the key is extended or not. You will be able to check it with an operation like this in the Windows Message loop.
  1. case WM_KEYDOWN:
  2. if ( ( LOWORD( lParam) & 0x01000000 ) == 0 ) // NOt an extended key
  3. {
  4. // Do Processing
  5. }
  6. else //Extended Key
  7. {
  8. //....
  9. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 1st, 2006
0

Re: Using HID Input

But I cannot use WM_KEYDOWN or something else, because I already use WM_INPUT. I recieve the input with WM_INPUT, not with WM_KEYDOWN.

When user hits ALT key, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYDOWN are sent. But, WM_SYSKEYUP is not sent.
Why? Why does WM_SYSKEYDOWN is sent twice?
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Mar 2nd, 2006
0

Re: Using HID Input

Quote originally posted by AhmedHan ...
But I cannot use WM_KEYDOWN or something else, because I already use WM_INPUT. I recieve the input with WM_INPUT, not with WM_KEYDOWN.

When user hits ALT key, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYDOWN are sent. But, WM_SYSKEYUP is not sent.
Why?
Quote originally posted by MSDN ...
WM_SYSKEYUP Notification

The WM_SYSKEYUP message is posted to the window with the keyboard focus when the user releases a key that was pressed while the ALT key was held down. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYUP message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lParam parameter.
Quote originally posted by AhmedHan ...
Why does WM_SYSKEYDOWN is sent twice?
I ran a piece of code and I didnt see this behaviour when I pressed the ALT key. It sent only the WM_SYSKEYDOWN and the WM_KEYUP messages.
  1. ALT Key
  2. Kbd: make=0038 Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0104 VK=0012
  3. Kbd: make=0038 Flags:0003 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=0012
Quote originally posted by AhmedHan ...
But I cannot use WM_KEYDOWN or something else, because I already use WM_INPUT. I recieve the input with WM_INPUT, not with WM_KEYDOWN.
Here is what I got for some keys.
  1.  
  2. DELETE Key
  3. Kbd: make=002a Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=00ff
  4. Kbd: make=0053 Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=002e
  5. Kbd: make=0053 Flags:0003 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=002e
  6. Kbd: make=002a Flags:0003 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=00ff
  7. 'K' Key
  8. Kbd: make=0025 Flags:0000 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=004b
  9. Kbd: make=0025 Flags:0001 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=004b
You can see that VK=00FF for the one of the KeyUp and key Down pairs of the Extended keys. I dont know if this is universal for all keyboards but for now you can try using it.If the 0xFF comparison does not work I think the values for Make or Flags must be sending that information, but couldn't find any documentation on it.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: execvp return help...
Next Thread in C Forum Timeline: Sorting from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC