Keyboard(s) detect

Started by archimede, June 05, 2022, 03:23:15 AM

Previous topic - Next topic

archimede

Hallo.
I have 3 keyboards connected to th computer.
I need to detect all keyboards connected and, when I press a key on a keyboard, to detect that key with the keyboard used.
Thank you very much

ChuckC

https://devblogs.microsoft.com/oldnewthing/20160627-00/?p=93755

The link above refers a blog article from an engineer at Microsoft where some sample C/C++ code is provided to demonstrate capturing raw input.  When capturing raw input, it is possible to distinguish between input received from multiple input devices of the same type, such as keyboards or mice.  Higher level handling of input such as the Windows messages generated for a GUI application that register keyboard input and mouse movement will not provide the source device information, so usage of raw input capture is necessary.  The C/C++ code could be adapted to C# code making use of PInvoke and then dynamically compiled within a WinBatch script.  Alternatively, WinBatch might possibly be able to utilize its DllCall() function, but that's somewhat "iffy" in this case.

stanl

Nice reply Chuck. That article does have some code snippets. Maybe the OP can experiment.

td

Quote from: ChuckC on June 06, 2022, 07:05:03 AM
https://devblogs.microsoft.com/oldnewthing/20160627-00/?p=93755

The link above refers a blog article from an engineer at Microsoft where some sample C/C++ code is provided to demonstrate capturing raw input.  When capturing raw input, it is possible to distinguish between input received from multiple input devices of the same type, such as keyboards or mice.  Higher level handling of input such as the Windows messages generated for a GUI application that register keyboard input and mouse movement will not provide the source device information, so usage of raw input capture is necessary.  The C/C++ code could be adapted to C# code making use of PInvoke and then dynamically compiled within a WinBatch script.  Alternatively, WinBatch might possibly be able to utilize its DllCall() function, but that's somewhat "iffy" in this case.

If the window creation business is ignored there doesn't appear to be anything in the sample that couldn't be handled with the judicious use of DllCall and DllCallBackCreate. Admittedly, it would be tricky and require a good amount of trial and error.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

archimede

Very interesting, but that is C language... :-(

JTaylor

Just another opportunity to learn  ;)

Jim

ChuckC

Not being able to understand the C/C++ language will be a major impediment to understanding the sample code that I posted a link to.

To put a question back in the OP's court:

What drives the need to know which keyboard was used to generate a key press event?

As far as Windows is concerned, it doesn't specifically care which device generated the event, and it abstracts the low-level interaction with a keyboard device so that applications are made aware of the key press event regardless of which device generated it.

archimede

I have much than 3 keyboards connected to the computer and I need to know which keyboard generates the character to send that character to the right window.