Using Raw Input
- Article
- 03/28/2021
- 2 minutes to read
-
-
Please rate your experience
Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy.
Thank you.
In this article
This section includes sample code for the following purposes:
- Registering for Raw Input
- Example 1
- Example 2
- Performing a Standard Read of Raw Input
- Performing a Buffered Read of Raw Input
Registering for Raw Input
Example 1
In this sample, an application specifies the raw input from game controllers [both game pads and joysticks] and all devices off the telephony usage page except answering machines.
Example 2
In this sample, an application wants raw input from the keyboard and mouse but wants to ignore legacy keyboard and mouse window messages [which would come from the same keyboard and mouse].
RAWINPUTDEVICE Rid[2]; Rid[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC Rid[0].usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE Rid[0].dwFlags = RIDEV_NOLEGACY; // adds mouse and also ignores legacy mouse messages Rid[0].hwndTarget = 0; Rid[1].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC Rid[1].usUsage = 0x06; // HID_USAGE_GENERIC_KEYBOARD Rid[1].dwFlags = RIDEV_NOLEGACY; // adds keyboard and also ignores legacy keyboard messages Rid[1].hwndTarget = 0; if [RegisterRawInputDevices[Rid, 2, sizeof[Rid[0]]] == FALSE] { //registration failed. Call GetLastError for the cause of the error }Performing a Standard Read of Raw Input
This sample shows how an application does an unbuffered [or standard] read of raw input from either a keyboard or mouse Human Interface Device [HID] and then prints out various information from the device.
Performing a Buffered Read of Raw Input
This sample shows how an application does a buffered read of raw input from a generic HID.
case MSG_GETRIBUFFER: // Private message { UINT cbSize; Sleep[1000]; VERIFY[GetRawInputBuffer[NULL, &cbSize, sizeof[RAWINPUTHEADER]] == 0]; cbSize *= 16; // up to 16 messages Log[_T["Allocating %d bytes"], cbSize]; PRAWINPUT pRawInput = [PRAWINPUT]malloc[cbSize]; if [pRawInput == NULL] { Log[_T["Not enough memory"]]; return; } for [;;] { UINT cbSizeT = cbSize; UINT nInput = GetRawInputBuffer[pRawInput, &cbSizeT, sizeof[RAWINPUTHEADER]]; Log[_T["nInput = %d"], nInput]; if [nInput == 0] { break; } ASSERT[nInput > 0]; PRAWINPUT* paRawInput = [PRAWINPUT*]malloc[sizeof[PRAWINPUT] * nInput]; if [paRawInput == NULL] { Log[_T["paRawInput NULL"]]; break; } PRAWINPUT pri = pRawInput; for [UINT i = 0; i < nInput; ++i] { Log[_T[" input[%d] = @%p"], i, pri]; paRawInput[i] = pri; pri = NEXTRAWINPUTBLOCK[pri]; } free[paRawInput]; } free[pRawInput]; }