Get background monitoring of barcode reader

I want to create an accounting program with C # language. I want to use a barcode reader to search for products in a store (this is not necessary for my program) Now, in the main form, if the seller uses a barcode reader, we will get the barcode value for the method or event of the descriptor; How can I get the barcode value in the background of the form (without a text field) for a method or processing event?

Note. My barcode reader is HID (USB interface).

+8
c # winforms
source share
1 answer

The barcode device behaves like a keyboard. When you have focus in the text box, it sends characters to the text box, as if you entered them from the keyboard.

If you do not want to use a text field, you need to subscribe to the keyboard event handler to capture the barcode stream.

Form1.InitializeComponent ():

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress); 

Handler and supporting elements:

 DateTime _lastKeystroke = new DateTime(0); List<char> _barcode = new List<char>(10); private void Form1_KeyPress(object sender, KeyPressEventArgs e) { // check timing (keystrokes within 100 ms) TimeSpan elapsed = (DateTime.Now - _lastKeystroke); if (elapsed.TotalMilliseconds > 100) _barcode.Clear(); // record keystroke & timestamp _barcode.Add(e.KeyChar); _lastKeystroke = DateTime.Now; // process barcode if (e.KeyChar == 13 && _barcode.Count > 0) { string msg = new String(_barcode.ToArray()); MessageBox.Show(msg); _barcode.Clear(); } } 

You will need to monitor the “keystrokes” and follow the “carriage return” that is sent with the barcode stream. This can easily be done in an array. To distinguish between keystrokes on a keyboard and keystrokes on a barcode, you can use one of the tricks you can do is keep track of the keystrokes.

For example, if you get a stream of keystrokes less than 100 ms, ending with a carriage return, you can assume that this is a barcode and a process, respectively.

Alternatively, if your barcode scanner is programmed, you can also send special characters or sequences.

+14
source share

All Articles