I have a simple USB barcode reader which I intend to use to scan COD 39 barcodes. I know that just scanning the barcode while having a text field on focus will get me the text input of the barcode I'm scanning. However my requirement is a bit different.

I need to create a C# application where upon reading a barcode it will check the read barcode with a list I have to see whether it exists. The problem is I cannot keep a text box on my app in focus all the time as the app does other things as well. Instead what I want is for my program to keep looking for some sort of "event" which will be raised once the bar code scanners scans a code.

So does anyone know if there is such an event which will be raised when the barcode is scanned some code so I can instruct my program to do the rest of the work when the scanners scans a barcode?

It depends on your type of scanner and whether you get drivers and API's with it.

It sounds like you have a keyboard emulation scanner. In which case, you will receive barcode input just as though someone typed it on a keyboard.
I suppose in this case your event would be "PreviewKeyDown" or something similar.

If your scanner supports Serial style communication (where you need to open up a port to get the data from the scanner) then this opens up a lot more possibility for you.

You will need to check the documentation for your scanner to see what you have available to you.

Thanks Ketsuekiame for reply.

Yes it is a keyboard emulation scanner and is listed under "Keyboards" in device manager. I bought it from Amazon Japan (yes I live there) and is a pretty cheap one so doesn't have much documentation except for a very vague User Manual.

Alright then I will take a look at the PreviewKeyDown event.

Member Avatar for Huntondoom

As far I know the scanner "hits" ENTER when done.
try to let it put its value inside notepad, and if it goes a line done then it means it hits ENTER

A lot of them do send "\r\n" after a scan by default, but this depends on how you set the scanner up and the scanner itself. The OP needs to read the instructions and documentation for the scanner. The USB scanners I use here can be configured with multiple or no key-presses after a successful scan.

But this isn't an "event" Per-Se. The OP will need to have their code monitoring the scanner input and as it comes from a keyboard emulator, there's no way to differentiate it from a normal keyboard input.

In my opinion, the OP was already using the best method by having the input go into a text box. This also allows you a fall back in case the scanner itself doesn't work or the barcode is malformed (due to damage or obstruction) it would allow the operator to type in the barcode number (provided it wasn't a 2D code)

A lot of them do send "\r\n" after a scan by default, but this depends on how you set the scanner up and the scanner itself. The OP needs to read the instructions and documentation for the scanner. The USB scanners I use here can be configured with multiple or no key-presses after a successful scan.

But this isn't an "event" Per-Se. The OP will need to have their code monitoring the scanner input and as it comes from a keyboard emulator, there's no way to differentiate it from a normal keyboard input.

In my opinion, the OP was already using the best method by having the input go into a text box. This also allows you a fall back in case the scanner itself doesn't work or the barcode is malformed (due to damage or obstruction) it would allow the operator to type in the barcode number (provided it wasn't a 2D code)

Yes it does send a "Return" at the end. I also found out that it is possible to configure the scanner so that it will add a either or both a prefix as well as suffix to the code before displaying on the computer so I guess I can use that to safeguard against unintentional key presses.

Thanks mate for the help!

Are you looking for some output sample codes to draw your barcode scanning result?I know how to draw barcode scanning result to data string.

 foreach (string aa in results)
            {
                Console.WriteLine(aa);
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.