It depends on the way you are interacting with the device. Anyway it wont be a C# solution, it will be some other library. Are you reading data from a stream? If you are just taking keystrokes, there may be nothing you can do about it.
Can you just unplug the keyboard?
RE Your edit: You can unplug the small ribbon cable on laptops. Just follow the manufacturer's directions for removing the keyboard, pull the ribbon out of the socket, and put the keyboard back in.
You could also try going into the keyboard control panel and uninstalling the keyboard driver it is using.
What I did in a similar situation is distinguish between a scan and a user typing by looking at the speed of the input.
Lots of characters very close together then a pause is a scan. Anything else is keyboard input.
I don't know exactly your requirements, so maybe that won't do for you, but it's the best I've got :)
Sorry, there's only one keyboard input - you'd have to delve into ring0 level stuff to figure out which keyboard (the scanner is a keyboard, as far as windows is concerned) a keypress came from since windows has very tight control over keyboard input. (for purposes of ctrl-alt-del, and security at login)
You might consider using a USB to PS2 keyboard dongle - you may be able to 'connect' to that HID device directly and ignore other keyboard input.
But C# isn't going to visit ring0 without an external library.
I think you might be able to distinguish multiple keyboards through DirectX API, or if that doesn't work, through raw input API.
Seems like you should be able to make the scanner send a code to say - "hey it's me!".
You could use the Raw Input API to distinguish between the keyboard and the scanner like I did recently. It doesn't matter how many keyboard or keyboard-like devices you have hooked up; you will see a WM_INPUT
before the keystroke is mapped to a device-independent virtual key that you typically see in a KeyDown
event.
Far easier is to do what others have recommended and configure the scanner to send sentinel characters before and after the barcode. (You usually do this by scanning special barcodes in the back of the scanner's user manual.) Then, your main form's KeyPreview
event can watch those roll end and swallow the key events for any child control if it's in the middle of a barcode read. Or, if you wanted to be fancier, you could use a low-level keyboard hook with SetWindowsHookEx()
to watch for those sentinels and swallow them there (advantage of this is you could still get the event even if your app didn't have focus).
I couldn't change the sentinel values on our barcode scanners among other things so I had to go the complicated route. Was definitely painful. Keep it simple if you can!