I want to create an accounting program with c# language. I want to use a barcode reader for searching products in shop (this is optional for my program) Now, in main form if seller uses a barcode reader get barcode value for handle method or event; How can I get barcode value in background of Form (without text box) for handle method or event ?
Note: My barcode reader is HID (USB interface)
You can use ML Kit to recognize and decode barcodes. There are two ways to integrate barcode scanning: by bundling the model as part of your app, or by using an unbundled model that depends on Google Play Services. If you select the unbundled model, your app will be smaller.
This free online barcode reader highlights the features and performance of the Cognex Mobile Barcode Scanner SDK, decoding images that contain barcodes of any of its supported symbologies. Get started by uploading your image to our server and check if it can be decoded.
The barcode device behaves like a keyboard. When you have focus in a textbox, it sends characters to the textbox as if you typed them from the keyboard.
If you dont want to use a textbox, you'll need to subscribe to a keyboard event handler to capture the barcode stream.
Form1.InitializeComponent():
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
Handler & supporting items:
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'll have to do keep track of the "keystrokes" and look out for the "carriage return" that is sent w/ the barcode stream. That can easily be done in an array. To differentiate between user keystrokes and barcode keystrokes, one dirty trick you can do is keep track of the timing of the keystrokes.
For example, if you get a stream of keystrokes less than 100ms apart ending w/ a carriage return, you can assume it is a barcode and process accordingly.
Alternatively, if your barcode scanner is programmable, you can also send special characters or sequences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With