使用 DirectInput 在 C# 中检索键盘状态?
当在特定窗体上按下某个键时,我需要相当快地检索键盘状态 - 速度足够快,以至于 Windows 窗体的标准 KeyUp/KeyDown 事件不够快。有人告诉我要尝试 DirectInput,但我没有使用该库的经验或任何线程经验,所以我根本不确定如何开始。
我如何设置一个线程,每次 DirectInput 获取键盘状态的变化时调用表单上的函数?我知道如何初始化设备,并且 SetEventNotification 几乎就是我想要的,但我不知道如何使用它。
任何帮助将不胜感激!
I need to retrieve the keyboard state when a key is pressed on a particular form rather quickly - fast enough that the standard KeyUp/KeyDown events for windows forms are not fast enough. I was told to try out DirectInput, but I have no experience working with the library or any threading experience, so I am unsure of how to start at all.
How would I set up a thread which called a function on the form back each time DirectInput picks up changes in keyboard state? I know how to initialize the device and that SetEventNotification is pretty much what I want, but I do not know how to use it.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案可能取决于您所说的窗口的按键向上/按键按下事件不够快的意思。它们当然以亚毫秒分辨率工作——这比大多数人的手指快得多——那么您希望通过“更快”地读取输入来实现什么好处呢?
您确定这不仅仅是您的事件循环或代码减慢了响应速度吗?
您可以使用 Windows 函数 GetAsyncKeyState/GetKeyState 实现与 DirectInput 轮询键盘状态类似(相同?)的速度。我没有使用最新版本的 DirectInput,但我很确定早期版本只是将相同的 Win32 事件用于非轮询键盘状态。 DirectInput 的主要优点是它提供了所有类型输入设备的通用抽象 - 它并不是真正用于“快速”键盘输入(无论这意味着什么)。
您可以在 Microsoft 自己的文档中看到 '使用不建议使用 DirectInput 进行键盘和鼠标输入...'
The solution probably depends on what you mean when you say that the key-up/key-down events for windows aren't fast enough. They certainly work at sub-millisecond resolution - which is massively faster than most peoples fingers - so what benefit do you hope to achieve by reading the input 'faster'?
Are you sure it's not just that your event loop or code is slowing down the response?
You can achieve similar (identical?) speeds to DirectInput's polling of the keyboard state using the windows functions GetAsyncKeyState/GetKeyState. I haven't used the latest version of DirectInput but I'm pretty sure that earlier versions just used the same Win32 events for non-polled keyboard state. DirectInput's main advantage is that it provides common abstractions over all kinds of input devices - it's not really for 'fast' keyboard input (whatever that means).
You can see on Microsoft's own documentation that 'The use of DirectInput for keyboard and mouse input is not recommended...'
这可能会帮助您不必使用 DirectInput:
http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx
希望有帮助
This may help you as in not having to use DirectInput:
http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx
hope it helps