Using HID Input

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

Using HID Input

 
0
  #1
Feb 28th, 2006
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. }
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: Using HID Input

 
0
  #2
Feb 28th, 2006
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.
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: Using HID Input

 
0
  #3
Mar 1st, 2006
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?
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: Using HID Input

 
0
  #4
Mar 1st, 2006
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. }
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: Using HID Input

 
0
  #5
Mar 1st, 2006
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?
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: Using HID Input

 
0
  #6
Mar 2nd, 2006
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?
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.
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
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.
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