问题描述
我试图确定什么时候我的键盘连接或断开。我曾尝试以下策略:
RegisterDeviceNotification
使用RegisterDeviceNotifaction作为的建议在how确定键盘的DirectInput 断开。当我尝试这样做,我得到 DB_DEVNODES_CHANGED
事件窗口中的回调提供任何其他信息(只是一些未知的设备改变连接状态)。我尝试过各种的GUID进行注册: 4D36E96B-E325-11CE-BFC1-08002BE10318
(一些网站说,这是GUID的键盘类)和检索的guidInstance IDirectInput8 :: EnumDevices(DI8DEVCLASS_KEYBOARD,...,DIEDFL_ATTACHEDONLY)
。但我仍然只得到了 DB_DEVNODES_CHANGED
事件。
IDirectInput8 :: EnumDevices
每个调用 IDirectInput8 :: EnumDevices(DI8DEVCLASS_KEYBOARD,...,DIEDFL_ATTACHEDONLY)
只列举了所连接的设备。然而,当我断开我的键盘,它仍然得到由 EnumDevices
枚举。当我做同样为我的游戏控制器(带类型 DI8DEVCLASS_GAMECTRL
)的控制器,只有当它连接列举,让我确定它的连接状态。
其他功能
我曾尝试:
-
IDirectInput8 :: GetDeviceStatus
-
IDirectInputDevice8 ::获得性能
-
IDirectInputDevice8 :: GetDeviceInfo
-
IDirectInputDevice8 :: GetDeviceState
-
IDirectInputDevice8 ::投票
所有功能成功,并提供没有洞察到,如果键盘连接。
我也通过MSDN的的,都无济于事。
问题
- 谁能谁做了这个成功的确认,我采取正确的方法呢?
- 有没有我可以俯瞰其他功能或API?
- 是举一个例子任何网站(我已经失败一派)?
- 为什么
EnumDevices
正常工作对我的控制器,但不是我的键盘?而如何让我为我的键盘工作?
你有没有考虑 GetRawInputDeviceList()
和 GetRawInputDeviceInfo()
,使用RID_DEVICE_INFO为pData所和检查它的dwType?
这将让你的初始状态,这时如果你的WndProc处理WM_INPUT_DEVICE_CHANGE可以使用PARAMS检测到添加/删除,和lParam的可以发送straigth到 GetRawInputDeviceInfo()
。
每注释:本WM_INPUT_DEVICE_CHANGE将只为呼吁应用到 RegisterRawInputDevices()
,并特别要求此通知
I am trying to determine when my keyboard is connected or disconnected. I have tried the following strategies:
RegisterDeviceNotification
Using RegisterDeviceNotifaction as explained on MSDN was suggested in how to determine keyboard disconnected in DirectInput. When I tried this I get DB_DEVNODES_CHANGED
event in the window callback which provides no other information (just some unknown device has changed connection state). I tried various GUIDs for the registration: 4D36E96B-E325-11CE-BFC1-08002BE10318
(several websites said this is the class guid for keyboards) and the guidInstance retrieved from IDirectInput8::EnumDevices(DI8DEVCLASS_KEYBOARD, ..., DIEDFL_ATTACHEDONLY)
. But I still only get the DB_DEVNODES_CHANGED
event.
IDirectInput8::EnumDevices
Each call to IDirectInput8::EnumDevices(DI8DEVCLASS_KEYBOARD, ..., DIEDFL_ATTACHEDONLY)
should only enumerate the devices that are attached. However, when I disconnect my keyboard, it still gets enumerated by EnumDevices
. When I do the same for my game controller (with the type DI8DEVCLASS_GAMECTRL
) the controller is only enumerated when it is attached, allowing me to determine its connection state.
Other Functions
I have tried:
IDirectInput8::GetDeviceStatus
IDirectInputDevice8::GetCapabilities
IDirectInputDevice8::GetDeviceInfo
IDirectInputDevice8::GetDeviceState
IDirectInputDevice8::Poll
All functions succeed and provide no insight into if the keyboard is connected.
I have also looked through MSDN's keyboard section, to no avail.
Questions
- Can anyone who has done this successfully confirm I'm taking the right approach?
- Is there another function or API I am overlooking?
- Any websites that give an example (I've googled unsuccessfully)?
- Why does
EnumDevices
work correctly for my controller but not my keyboard? And how do I make it work for my keyboard?
Have you considered GetRawInputDeviceList()
and GetRawInputDeviceInfo()
, using RID_DEVICE_INFO for pData and checking it's dwType?
That will get you the initial state, then if your wndProc handles WM_INPUT_DEVICE_CHANGE you can use the params to detect add/remove, and the lParam can be sent straigth to GetRawInputDeviceInfo()
.
Per comments: The WM_INPUT_DEVICE_CHANGE will only arrive for apps that have called RegisterRawInputDevices()
and specifically asked for this notification.
这篇关于Win32的确定连接键盘时/断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!