Related: How do I read input from a USB HID device?
Related article on CodeProject: http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard
Article on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645543(v=vs.85).aspx
This looks like a possible duplicate, but I'll summarize here to avoid link rot:
Since you indicate your barcode scanner is a USB device that behaves as an HID Keyboard, and you want your program's textbox to accept input from only this one particular device, you can use the HID Raw Input
technique. However, this requires processing the raw HID reports from the device.
During initialization, your software registers for Raw Input
from an HID input device having a specific Top Level Collection (TLC).
Use GetRawInputDeviceList
to enumerate the available raw input devices, and use GetRawInputDeviceInfo
to find each device's name. Within the name string you should be able to match the USB VID/PID values, so you can identify whether a keystroke message comes from the barcode scanner. Be sure to save the matching device handle hDevice
discovered by this enumeration.
Extend your windowproc to handle WM_INPUT
messages by GetMessage
and GetRawInputData
, and filter out messages from the standard keyboard by matching the raw input handle against the previously determined device handle of the barcode scanner.