viewpoint-particle

Author Topic: Keyboard(s) detect  (Read 293 times)

archimede

  • Full Member
  • ***
  • Posts: 112
Keyboard(s) detect
« on: June 05, 2022, 03:23:15 am »
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

  • Sr. Member
  • ****
  • Posts: 393
Re: Keyboard(s) detect
« Reply #1 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.

stanl

  • Pundit
  • *****
  • Posts: 1656
Re: Keyboard(s) detect
« Reply #2 on: June 06, 2022, 07:21:39 am »
Nice reply Chuck. That article does have some code snippets. Maybe the OP can experiment.

td

  • Tech Support
  • *****
  • Posts: 4094
    • WinBatch
Re: Keyboard(s) detect
« Reply #3 on: June 06, 2022, 08:21:37 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

  • Full Member
  • ***
  • Posts: 112
Re: Keyboard(s) detect
« Reply #4 on: June 07, 2022, 03:50:32 am »
Very interesting, but that is C language... :-(

JTaylor

  • Pundit
  • *****
  • Posts: 1832
    • Data & Stuff Inc.
Re: Keyboard(s) detect
« Reply #5 on: June 07, 2022, 07:03:51 am »
Just another opportunity to learn  ;)

Jim

ChuckC

  • Sr. Member
  • ****
  • Posts: 393
Re: Keyboard(s) detect
« Reply #6 on: June 07, 2022, 12:50:05 pm »
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

  • Full Member
  • ***
  • Posts: 112
Re: Keyboard(s) detect
« Reply #7 on: June 10, 2022, 03:59:06 pm »
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.