We help IT Professionals succeed at work.

C# Best method for capturing keyboard wedge barcode scanner data

locdang asked
Hi everyone,

I am writing a c# app that uses both user input and data retrieved from a keyboard wedge barcode scanner. Basically I have the application working at the moment by having the scanner scan into a textbox and detecting a carriage return as the barcode data terminator.

My problem is this application is run on a machine that has no keyboard / mouse only a number pad and a scanner. If (for whatever reason) the application loses focus to something else the barcode scanner does not work as the text box is no longer focused. Whilst we 'could' have a mouse also attached it would defeat our goal of minimal attached devices and increases the duties of the operating staff (I know clicking in a textbox isn’t a huge bit of labour but I am sure you all know that the customer / end user will complain about every 'unnecessary' mouse click they have to perform. We're also perfectionists :D)

A couple ideas I have had are:
Constantly evaluating and setting the application as the focused application
Using a low level keyboard hook

My problem with the auto focus method is that it may interfere with things like the task manager launching and also feels like more of a 'hack' than a solution and with the keyboard hook having the scanner hook play up with user data on the numerical keypad.

I am very interested to hear how others have combated this issue.


Watch Question


Just a thought, most barcode scanning devices can be configured with a prefix as well as suffix (in your case the enter key) so what you can do is override on key press in your form and check for the prefix, you then know for each keypress is part of the barcode until enter is pressed. You can capture these string of key presses and then assign them to the text box or process as you would on leaving the text box.

Hope that helps


I gave you the wrong method to override you want to override the following.

// Parameters:
        //   msg:
        //     A System.Windows.Forms.Message, passed by reference, that represents the
        //     window message to process.
        //   keyData:
        //     One of the System.Windows.Forms.Keys values that represents the key to process.
        // Returns:
        //     true if the character was processed by the control; otherwise, false.
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData);

Open in new window



Unfortunately the scanners we are currently using cannot be programmed to do so mainly because they are really cheap ones that we got for the proof of concept and initial implementation (we had a very restrictive budget) and I cannot get funding for decent scanners until the product is tested, working and already inproduction.

I agree though, listening for a prefix then suffix and using your solution would be a great idea, but it cannot work at this time.

Any other ideas? :)

So is there just the one textbox on the form? if so you can just assume my above suggestion if not hows the user navigating?
What barcodes are you using eg if you are using upc (or another format you can parse with known rules) then you can capture all key presses count back 12 characters after your enter key pressed and theres your barcode.

Does that help?