16

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 they are not looking at.

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

Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
  • I have answered this question here: [How to detect the input device if mouse and touchpad are both enabled](http://stackoverflow.com/a/8804862/1000282). The answer uses a c library and drivers I have created. – oblitum Feb 16 '12 at 19:23
  • Not strictly an answer to my question. The answer marked as correct has worked fine for several years. – Ray Hayes Feb 24 '12 at 11:28
  • 1
    @ray-haves Can you confirm whether you are able prevent input from _making_ for other applications even after you have filtered it. That's, once a keystroke is filtered through raw input, are you able to stop it from being processed by the rest of the operating system? – oblitum Feb 24 '12 at 19:14
  • sorry, wrong target, @ray-hayes – oblitum Feb 24 '12 at 19:41
  • @Chico, no, it doesn't prevent the key from being propagated to other applications. But that wasn't the intent of my question. I had a special hardware device that needed to feed its key-presses to an application (full screen on a separate monitor) regardless as to whether it was focused or not. Sure, if they play with the keys when they have Microsoft Word focused, they're going to get unusual results - but as it happens, that isn't very like due to the way things are physically set up. Whilst your answer may be _better_ , the one implemented works fine for now! ;-) – Ray Hayes Feb 27 '12 at 15:51
  • Thanks to confirm that behavior ;-) – oblitum Feb 27 '12 at 17:29

2 Answers2

16

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.

Roel
  • 19,338
  • 6
  • 61
  • 90
  • I have similar issue now that I need to get multiple touch events. do you happen to have more detailed example? – user1865027 Aug 04 '17 at 02:05
-3

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

Roel
  • 19,338
  • 6
  • 61
  • 90
  • 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 Sep 18 '08 at 10:36
  • 1
    Unaccepting this as it does look like it is possible without a driver based upon A Nony Mouse's reply. – Ray Hayes Sep 18 '08 at 11:10