I have a system where I have multiple keyboards and really need to know which keyboard the key stroke is coming from. To explain the set up:

  1. I have a normal PC and USB keyboard
  2. I have an external VGA screen with some hard-keys
  3. The hard keys are mapped as a standard USB keyboard, sending a limited number of key-codes (F1, F2, Return, + and -)

I have a low-level hook (in C# but actually calling upon Win32 functionality) which is able to deal with the input even when my application is not focused.

The problem is that when using the normal keyboard, some of the mapped key-codes at picked up by the application being driven on the external screen. One of the key-presses sent by the external screen and used for confirmation is VK_RETURN. Unless I can identify the "device" and filter upon it, the user could be performing actions and confirming them on a screen their not even looking at.

How do I know which keyboard was responsible for the key-press?


not being a windows internals man, I'm only guessing that you should wrap the driver of the keyboard (/screen keys) you want to capture.

Amir Arad
I really want to avoid getting to the level of writing a driver. Kernel mode programming is a whole area I'd be keen to avoid.
Ray Hayes

No way to do this. Windows abstracts this for you. As mentioned, you need to write/modify a device driver.

Oh well, that's a shame. I'm going to get on to the device manufacturers and see if there is a way of flashing the hardware to use different key-codes (hopefully up in the high VK_Fn numbers).
Ray Hayes
Unaccepting this as it does look like it is possible without a driver based upon A Nony Mouse's reply.
Ray Hayes
+1  A: 

There is a way to do this, I had It working using quick macros and keyboard detector.

A Nony Mouse
Looks like you're right. I just need to understand the qml logic to try and work out how it's doing it!
Ray Hayes
+5  A: 

Yes I stand corrected, my bad, learning something new every day.

Here's my attempt at making up for it :) :

  • Register the devices you want to use for raw input (the two keyboards) with ::RegisterRawInputDevices().

  • You can get these devices from GetRawInputDeviceList()

  • After you've registered your devices, you will start getting WM_INPUT messages.

  • The lParam of the WM_INPUT message contains a RAWKEYBOARD structure that you can use to determine the keyboard where the input came from, plus the virtual keycode and the type of message (WM_KEYDOWN, WM_KEYUP, ...)

  • So you can set a flag of where the last message came from and then dispatch it to the regular keyboard input handlers.

+3  A: 

I was looking for a solution to a similar problem today and found a complete and working solution here, including source code.

Using Raw Input from C# to handle multiple keyboards

Mm - you may have saved my life with this answer! =)
Erik Forbes