WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: archimede on June 05, 2022, 03:23:15 AM

Title: Keyboard(s) detect
Post by: archimede 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
Title: Re: Keyboard(s) detect
Post by: 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.
Title: Re: Keyboard(s) detect
Post by: stanl on June 06, 2022, 07:21:39 AM
Nice reply Chuck. That article does have some code snippets. Maybe the OP can experiment.
Title: Re: Keyboard(s) detect
Post by: td on June 06, 2022, 08:21:37 AM
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.
Title: Re: Keyboard(s) detect
Post by: archimede on June 07, 2022, 03:50:32 AM
Very interesting, but that is C language... :-(
Title: Re: Keyboard(s) detect
Post by: JTaylor on June 07, 2022, 07:03:51 AM
Just another opportunity to learn  ;)

Jim
Title: Re: Keyboard(s) detect
Post by: ChuckC 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.
Title: Re: Keyboard(s) detect
Post by: archimede 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.
Title: Re: Keyboard(s) detect
Post by: ChuckC on June 12, 2022, 10:39:51 AM
Here is some foundational reference material that you need to read and develop an understanding of:

https://docs.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows

https://docs.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input

https://docs.microsoft.com/en-us/windows/win32/inputdev/keyboard-input

https://docs.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input

https://docs.microsoft.com/en-us/windows/win32/inputdev/using-keyboard-input

https://docs.microsoft.com/en-us/windows/win32/inputdev/keyboard-input-reference