使用GetRawInputBuffer进行原始输入读取 [英] Using GetRawInputBuffer for Raw Input reads

查看:394
本文介绍了使用GetRawInputBuffer进行原始输入读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XP/SP2上的Raw Input处理来自HID的WM_INPUT消息.

我已经在MSDN文档中为GetRawInputBuffer编写了示例,但是第一次调用是pData == NULL始终返回,返​​回值为0,而pcbSize ==0.

使用GetRawInputData(标准读取)技术,可以使用完全相同的代码.

任何人都可以验证该文档是否正确.或发布更多有用的示例?


谢谢

推荐答案

我最近遇到了这个问题.在我写了一段工作代码之后,我有以下观察

I've come across this problem too lately. After I wrote a piece of working code, I have the following observation

使用WM_INPUT时,请始终使用GetRawInputData

When you use WM_INPUT, always use GetRawInputData

如果在WM_INPUT内使用GetRawInputBuffer,它可能会在WM_INPUT的rawinput之后(在消息队列中,尚未被GetMessage或PeekMessage提取)获取原始输入.如果您正在用鼠标阅读,使用GetRawInputBuffer始终不会得到任何结果,因为我认为Windows会尝试将多个rawinput连接成一个.

我发现一个案例,您可以很容易地产生一个案例,无需过多修改即可从GetRawInputBuffer获取结果.将您的GetRawInputBuffer代码放入消息循环中.我使用PeekMessage,但是我认为这没有关系.在应用程序运行期间,按ALT键,让应用程序进入菜单循环(这样您的消息就会停留在某个位置),移动鼠标,然后再次按ALT键.然后GetRawInputBuffer会得到一些结果.

I found a case that you can easily produce a case that you can get results from GetRawInputBuffer without much modification. Put your GetRawInputBuffer code inside the message loop. I use PeekMessage, but I think it doesn't matter. During application running, press ALT key, to let the application enter the menu loop (so your message will get stuck in some point), move your mouse, then press ALT key again. The GetRawInputBuffer will then get some result.

我使用GetRawInputBuffer的工作代码大多数事情发生在单独的线程中.单独的线程执行以下操作:

My working code of using GetRawInputBuffer has most of the things happened in a separate thread. The separate thread does the following:

1.创建一个不可见的弹出式窗口

  1. create an invisible popup style window

2.将原始输入设备注册为RAWINPUTDEVICE结构的hwndTarget设置为从1创建的hwnd.

  2. register rawinput devices with hwndTarget of the RAWINPUTDEVICE structure set to the hwnd created from 1.

3.等待直到收到信号

  3. Wait until getting signaled

4.使用GetRawInputBuffer检索原始输入

  4. use GetRawInputBuffer to retrieve rawinputs

5.返回3.如果未发出退出线程的信号

  5. back to 3. if not signaled to exit thread

6.在2.中注销设备.

  6. deregister the devices in 2.

所以,假设您的主线程更新函数将以60 Hz的频率被调用,在更新函数内部,向单独的线程发出信号,等到它完成,现在您便获得了rawinputs

So, let's say your main thread update function will get called at 60 Hz, inside the update function, signals the separate thread, wait till it finishes, now you get the rawinputs

我读到某处DirectInput创建了单独的线程来处理输入.在Xp情况下,我认为DirectInput正在使用RAWINPUT api.我想几乎不可能严重干扰您的应用程序代码来使用GetRawInputBuffer

I read somewhere that DirectInput creates separate threads to handle inputs. In Xp case I think that DirectInput is using RAWINPUT api. I guess that it is almost impossible to severely interfere your application code to use GetRawInputBuffer 

另一个发现,我发现我可以在消息循环内的窗口proc外部解释WM_INPUT,但是我认为这样做不是一个好习惯

Another finding, I found that I can interpret the WM_INPUT outside window proc, that is inside the message loop, but I think it is not a good practice to do so

因此,最后,GetRawInputData更加简单,无需弄乱多线程

So at the end, GetRawInputData is easier, no need to mess with multithreads


这篇关于使用GetRawInputBuffer进行原始输入读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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