如何检测键盘是否已连接



我有一个MFC应用程序,有时可以在触摸屏上工作如果键盘没有连接,我想在聚焦编辑框时打开我的虚拟键盘

以下是使用GetRawInputDeviceList检查是否存在RIM_TYPEKEYBOARD设备的快速尝试。虽然我在错误处理方面略作介绍,但希望它在哪里需要它是显而易见的。这确实检测到我正在移除USB键盘。

bool detectKeyboard() {
bool bHasKeyboard = false;
UINT nDevices;
UINT ret = GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST));
if (ret == 0) {
PRAWINPUTDEVICELIST pRawInputDeviceList = new RAWINPUTDEVICELIST[nDevices];
ret = GetRawInputDeviceList(pRawInputDeviceList, &nDevices,
sizeof(RAWINPUTDEVICELIST));
if (ret != (UINT)-1) {
for (UINT i = 0; i < nDevices; ++i) {
if (pRawInputDeviceList[i].dwType == RIM_TYPEKEYBOARD) {
bHasKeyboard = true;
break;
}
}
}
// else error calling GetRawInputDeviceList to fetch the devices list
delete[] pRawInputDeviceList;
}
// else error calling GetRawInputDeviceList to return number of devices
return bHasKeyboard;
}

(我已经有一段时间没有写C++或MFC了,所以为任何糟糕的风格道歉。(

最新更新