如何在C ++/CLI中读取原始输入 [英] How Do I Read Raw Input in C++/CLI

查看:70
本文介绍了如何在C ++/CLI中读取原始输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从连接的设备获取原始输入.我正在使用MSDN 可以弄清楚,但对于让我理解其中的一些内容.

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?

头文件中的重要内容:

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

CPP文件中的重要内容:

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));

如何继续?

我对C ++有点生锈,您在这里看到的几乎是我对原始输入知识的总和.我不知道它是否会起作用,但是我使用的是C ++/CLI,而不是常规的C ++.我如何才能从中获得某种无缓冲的原始输入(最好是从键盘输入)?

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)?

我发现的大多数示例都有一个switch语句.我不明白它是如何工作的.我有这样的东西:

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;
}

msg变量如何工作?如何正确创建和分配一个?

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

推荐答案

根据

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

与系统相连的设备的RAWINPUTDEVICELIST结构的数组.如果为NULL,则在* puiNumDevices中返回设备数.

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

这是返回值的文档:

如果函数成功,则返回值是pRawInputDeviceList指向的缓冲区中存储的设备数.

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

您要为第一个参数传递null,并期望方法返回的设备数量.文档没有说会这样做.

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?

在您的评论中,您仍将返回值存储在numDevices中.另外,您将第二个参数用作数组.长度为1的数组基本上是相同的东西,但最好将其视为指向整数的指针.

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.

这是MSDN页面上的示例代码的副本,其中添加了我的注释.尝试一下.

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);

这篇关于如何在C ++/CLI中读取原始输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆