4

I need to differentiate between a keypress of (at least) two usb-keyboards connected to a single PC. Is there a method/precedure available to decide if key 's' (for example) is pressed either by keyboard A or Keyboard B? (preferable language is C#)

Pseudo-Code:

if (keyPressed == 's')
{ 
   if(keyboardID == 'A')
   {
       print("Key 's' was pressed by Keyboard A");
   }
   else if (keyboardID == 'B')
   {
       print("Key 's' was pressed by Keyboard B");
   }
}
  • I would recommend providing more context for your question. Is this going to run as a console application or a win forms application etc. – Theo Jan 25 '17 at 20:55
  • 1
    I'll be watching this for the answer, as I've been a bit curious myself (as well as with mice). I'm sure it involves specifically identifying the address of the USB port being used, the IRQ, or similar. – CDove Jan 25 '17 at 20:56
  • 1
    windows or linux? – eactor Jan 25 '17 at 21:03
  • http://superuser.com/questions/177305/using-multiple-keyboards-with-different-keyboard-layouts-in-windows – eactor Jan 25 '17 at 21:05
  • There is no limit within the context, meaning it doesn't matter if the OS is Windows or Linux... I can choose the OS running on host-PC. Moreover, I can decide if it’s a console application or a win forms application. When I think out of the box... it is also possible to install two VMs, attach each keyboard to a VM, and code a program which communicated from the VM via TCP-sockets to my host-PC... But maybe there is a complete different easier way? – Stephan Radke Jan 25 '17 at 21:15
  • /dev/input/eventx -> http://stackoverflow.com/questions/5060710/format-of-dev-input-event – eactor Jan 25 '17 at 21:30

2 Answers2

1

keystrokes are processed by the OS as events

in linux for every input device there is separate event queue in /dev/input/event* (* means the specific device number)

in linux you can list all input devices with the command sudo lsinput (input-utils)

you can capture the specific events for each of your keyboards, see Linux keyboard event capturing /dev/inputX

in windows this is similar. you can use rawInput and get a handle for each device. deeper information and example codes is in

https://blogs.msdn.microsoft.com/oldnewthing/20160627-00/?p=93755 ("The device handle tells us which keyboard generated the input... ")

https://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard

raw input handling (distinguishing secondary mouse)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645543%28v=vs.85%29.aspx

Community
  • 1
  • 1
ralf htp
  • 8,134
  • 3
  • 19
  • 30
0

Short of getting to the driver level and intercepting the keystroke there, you really can't (I think). I utilize a bluetooth keyboard and a bluetooth keypad and could not find a way to do what you are asking so the number 3 on the keypad would register different than the number 3 on the keyboard keypad.

Jon Glazer
  • 717
  • 1
  • 4
  • 21