| | |
Using HID Input
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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...
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...
C Syntax (Toggle Plain Text)
class HIDInput { private: RAWINPUTDEVICE Rid; public: LPARAM hRawInput; HWND hTargetWnd; USHORT LastKey; void Register(void) { Rid.usUsagePage = 0x01; Rid.usUsage = 0x06; Rid.dwFlags = RIDEV_INPUTSINK; Rid.hwndTarget = hTargetWnd; if (RegisterRawInputDevices(&Rid, 1, sizeof(Rid)) == FALSE) DebugTest("Error registering input"); } unsigned int GetData(void) { UINT cbSize; GetRawInputData((HRAWINPUT) hRawInput, (UINT) RID_INPUT, (LPVOID) NULL, (PUINT) &cbSize, (UINT) sizeof(RAWINPUTHEADER)); LPBYTE lpbBuffer = new BYTE[cbSize]; GetRawInputData((HRAWINPUT) hRawInput, (UINT) RID_INPUT, (LPVOID) lpbBuffer, (PUINT) &cbSize, (UINT) sizeof(RAWINPUTHEADER)); RAWINPUT * pRawInput = (RAWINPUT *) lpbBuffer; if (pRawInput->header.dwType == RIM_TYPEKEYBOARD) return LastKey = pRawInput->data.keyboard.VKey; //This one never returns 0 else return 0; //Error }
use the 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
As for the user keeping on pressing, Maybe checking for consecutive 0x100 values with the same VKey will do the trick.
C Syntax (Toggle Plain Text)
pRawInput->data.keyboard.Message
something like
C Syntax (Toggle Plain Text)
if ( pRawInput->data.keyboard.Message = 0x100 ) // Pressed { // Do processing } else { //Ignore }
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?
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?
•
•
•
•
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?
C Syntax (Toggle Plain Text)
case WM_KEYDOWN: if ( ( LOWORD( lParam) & 0x01000000 ) == 0 ) // NOt an extended key { // Do Processing } else //Extended Key { //.... }
•
•
•
•
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?
C Syntax (Toggle Plain Text)
ALT Key Kbd: make=0038 Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0104 VK=0012 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.
C Syntax (Toggle Plain Text)
DELETE Key Kbd: make=002a Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=00ff Kbd: make=0053 Flags:0002 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=002e Kbd: make=0053 Flags:0003 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=002e Kbd: make=002a Flags:0003 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=00ff 'K' Key Kbd: make=0025 Flags:0000 Reserved:0000 ExtraInformation:00000000, msg=0100 VK=004b Kbd: make=0025 Flags:0001 Reserved:0000 ExtraInformation:00000000, msg=0101 VK=004b
![]() |
Similar Threads
- HID Input (C)
- System 32 Coming Up at Startup (Viruses, Spyware and other Nasties)
- I can't remove about:blank (Viruses, Spyware and other Nasties)
- hijackthis log...I need help please (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: execvp return help...
- Next Thread: Sorting from a file
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






