Search code examples
c#winformsbarcode-scanner

Windows Forms Barcode Scanning C#


I bought this barcode scanner off Amazon in hopes I could use it in my application.

To put it simply I would like my application to run a process when the scanner picks up a barcode. I will be making the barcodes myself.

The issue is that the scanner works by Copy+Pasting the barcode into any text field.

I used a keyboard tester and it directly "types" the code and hits enter.

enter image description here

Is there a way I can listen to this operation?


Solution

  • You can add a key down listener to your program form and then handle the input. If the barcode-scanner is "typing" the keys you will get an event for every key. To do this:

    set the Form.KeyPreview Property to true. This property gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

    And then adding this function:

    yourForm.KeyDown += new System.Windows.Forms.KeyEventHandler(yourForm.yourForm_KeyDown);
    
    private void yourForm_KeyDown(object sender, KeyEventArgs e)
    {
         //handle input here
    }