I'm working on an application that uses Window's window and raw input APIs.
I can get input without too much difficulty and right now I'm trying to detect when devices are connected or disconnected from the application. To do so I'm listening to WM_INPUT_DEVICE_CHANGE and using GetRawInputDeviceInfo() to fetch device information.
My current issue is that, when having a mouse and keyboard connected, the OS detects three devices: two keyboards and a mouse. The mouse works fine but, due to having two different keyboards, I can't identify which one is actually the "main" one. When the mouse or keyboard are disconnected, one of the keyboard devices is removed. In order to have no keyboards, all devices must be disconnected.
Is there any simple way for detecting which keyboard is the "main" one? Or there isn't any concept of a "main" keyboard on this API?
Right now I have though the following "solutions":
First one would be to store the device handle when a key is pressed and keep it as the main keyboard but this sounds a bit "hacky".
Second one would be to try to differentiate the "main" keyboard based on number of keys. Issue is that this also seems a bit hacky as keyboard could vary in number of keys easily.
Although I don't deem it necessary, I'm providing a bit of code about how I'm checking connection and disconnection status for devices, just for completion.
// Read LPARAM from WM_INPUT_DEVICE_CHANGE and fetch the information.
HANDLE DeviceHandle = (HANDLE)LParam;
bool bIsConnected = WParam == GIDC_ARRIVAL;
if(!bIsConnected)
return;
// Get the device data.
RID_DEVICE_INFO DeviceInfoData;
UINT SizeData = sizeof(RID_DEVICE_INFO);
UINT DeviceResult = GetRawInputDeviceInfo((void*)LParam, RIDI_DEVICEINFO, &DeviceInfoData, &SizeData);
// Launch any callback based on device type read from dwType variable inside DeviceInfoData.
Many thanks :)