Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log second keyboard events

Tags:

c++

c

windows

I have my own keyboard and an USB barcode scanner that works like a second keyboard.

I would like to use the main keyboard to control the computer (as you usually do) and the second keyboard (that is actually the barcode scanner) to log all the input into a file.

Is this possible to do?

The point is, I could be on the internet, word, excel or whatever. I would use the main keyboard to write to that processes while in the background the second keyboard (barcode scanner) could be at the same time writing but to a log file. The program that I could be using in the moment would never know about the second keyboard input.

enter image description here

Thanks, all suggestions are very welcome.

like image 795
Daniel Oliveira Avatar asked Feb 16 '17 23:02

Daniel Oliveira


1 Answers

You can use the Raw Input API to monitor keyboard events before the OS processes them. The API will tell you which device is sending each event, so you can log events from just the scanner.

However, the Raw Input API does not allow you to block input, so to block the scanner's events from being processed as normal keyboard events, you would need to use SetWindowsHookEx() to setup a keyboard hook that dismisses the events.

However, SetWindowsHookEx() does not report which device is sending each event, so you will have to coordinate the two APIs manually. When Raw Input detects a keyboard event, set a flag based on which device the event came from. When the hook detects the cooresponding event, check the flag and dismiss the event if the flag indicates the scanner device.

See Combining Raw Input and keyboard Hook to selectively block input from multiple keyboards on CodeProject.

like image 77
Remy Lebeau Avatar answered Oct 07 '22 22:10

Remy Lebeau