A: 

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.

Karl
A: 

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.

Geoffrey Chetwood
+3  A: 

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 :)

Blorgbeard
Yeah, finding and reading from the device directly is non trivial. You should be able to set the scanner up to give start and stop sequences as well - these sequences would never occur on the keyboard.
Adam Davis
+1  A: 

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.

Adam Davis
A: 

I think you might be able to distinguish multiple keyboards through DirectX API, or if that doesn't work, through raw input API.

(note: a barcode scanner appears as just another keyboard to Windows)
A: 

Seems like you should be able to make the scanner send a code to say - "hey it's me!".

Sam
+2  A: 

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!

Nicholas Piasecki
I am trying to follow along with the article. However, I get an error NativeMethods is inaccessible due to its protection level. It seems as though I need to import a dll; is this correct? If so, how do I do it? Also, which protected override void WndProc(ref Message m) definition should I use?
Amar Patel
Also am getting an error related to [SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]error CS0246: The type or namespace name 'SecurityPermission' could not be found (are you missing a using directive or an assembly reference?)
Amar Patel
Along with an error on the line containing: if ((from hardwareId in hardwareIdswhere deviceName.Contains(hardwareId) select hardwareId).Count() > 0)Error is error CS1026: ) expected
Amar Patel
Should I be placing all the code in the Distinguishing Barcode Scanners from the Keyboard in WinForms article in one .cs file called BarcodeScannerListener.cs?
Amar Patel
NativeMethods is a class that was in my project that contained all of the necessary P/Invoke declarations. You might get the LINQ error if you're not on .NET 3.5. I'll try to de-proprietize the code sometime this week and upload it to my blog. You would use the second WndProc.
Nicholas Piasecki
Amar, I added a link to a sample file. You should find the blog article easier to follow along with the complete code file. It's at the bottom of the posting. Public domain, no warranties--have at it and good luck!
Nicholas Piasecki
I was not able to open the solution in VS 2005, so I downloaded Visual C# 2008 Express Edition, and the code ran. However, after hooking up my barcode scanner and scanning a barcode, the program did not recognize the scan. I put a break point in OnBarcodeScanned method but it never got hit.
Amar Patel
I did change the App.config with the id of my Barcode scanner obtained using Device Manager.
Amar Patel
Amar Patel