How Do I Read Raw Input in C++/CLI

旧街凉风 提交于 2021-01-29 16:52:11

问题


Here's my problem.

I am trying to get raw input from attached devices. I am using the information on MSDN to figure it out, but it's been very difficult for me to make sense of some of it.

I believe I have successfully created a RAWINPUTDEVICE object and registered a device with RegisterRawInputDevices(). Now I am trying to figure out how to use the GetRawInputDeviceList() and GetRawInputDeviceInfo() functions. The big trouble is trying to understand the parameters they take, which includes a RAWINPUTDEVICELIST object, a HANDLE, a LPVOID, and a PUINT.

What are those variables and how do I use them?

Here's what I've gotten to work:

Important stuff in the header file:

#include <Windows.h>
#include <Winuser.h>

Important thing in the CPP file:

// I do not know where I found this not what it does, but it fixed some errors
   // that I could not solve. MSDN did not mention it.
#pragma comment(lib, "user32.lib")

And the stuff that works, and the thing that does not:

RAWINPUTDEVICE rid[1];

rid[0].usUsagePage = 1;
rid[0].usUsage = 6;
rid[0].dwFlags = 0;
rid[0].hwndTarget = NULL;

// awesomeSauce returned true, so it works
bool awesomeSauce = RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE) );

// Nothing past this point works
UINT numDevices = GetRawInputDeviceList(NULL, NULL, sizeof(RAWINPUTDEVICELIST));

How do continue?

I am a little bit rusty at C++, and what you see here is pretty much the sum of my knowledge with raw input. I do not know if it will effect anything, but I am using C++/CLI, not regular C++. How do I go from this, to getting some kind of unbuffered raw input (preferably from the keyboard)?

Edit:

Most examples I find have a switch statement. I don't understand how it works though. I have something like this:

UINT msg; // How does this work?
switch(msg)
{
case WM_CREATE:
    executeCase = 1;
    break;
case WM_INPUT:
    executeCase = 2;
    break;
}

How does the msg variable work? How can I create and assign one correctly?


回答1:


According to the MSDN page for GetRawInputDeviceList, here's the documentation for the first parameter (which you're passing as null):

An array of RAWINPUTDEVICELIST structures for the devices attached to the system. If NULL, the number of devices are returned in *puiNumDevices.

And here's the documentation for the return value:

If the function is successful, the return value is the number of devices stored in the buffer pointed to by pRawInputDeviceList.

You're passing null for the first parameter, and expecting the number of devices to be present in the return from the method. The documentation doesn't say it will do that.

Pass an actual variable for the second parameter, and read that instead of the return code.

If that doesn't solve your problem, please be more specific with "this is the thing that doesn't work". Tell us how it doesn't work: does it throw an exception, is there an error return code, does a black hole open up and swallow your computer whole?


Edit

In your comment, you're still storing the return value in numDevices. Also, you're using the second parameter as an array. An array of length 1 is basically the same thing, but it'd be better to think of it as a pointer to an integer.

Here's a copy of the sample code from the MSDN page, with my comments added in. Give this a try.

UINT nDevices;
PRAWINPUTDEVICELIST pRawInputDeviceList;

// Pass a null pointer to find out how many devices there are.
if (GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVCELIST)) != 0) { Error();}

// Now, malloc the needed storage, based on the output parameter, NOT the return value.
if ((pRawInputDeviceList = malloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) == NULL) {Error();}

// Finally, call for real, passing in the newly allocated buffer.
if (GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST)) == (-1) {Error();}


// after the job, free the RAWINPUTDEVICELIST
free(pRawInputDeviceList);


来源:https://stackoverflow.com/questions/17457461/how-do-i-read-raw-input-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!