[SOLVED] Scanning barcodes in WPF application


I have been searching for a while but can not seem to find an answer..

I have a bar code scanner hooked up to my computer and it acts as a keyboard wedge. The Scanner is setup to send me a StartOfTransmission and EndofTransmission character so that I know to process all the keys in-between and do some action.

in the shell of my application I have the following code…

private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
    e.Handled = BarcodeScanner.Instance.ProcessTextInput(this, e.Text, e.Device, e.Timestamp);

This works great if I have focus on the main window OR a control that does not process spaces (label, etc)

The issue that I have come across is that the barcode (not controlled by me) has spaces in it. If the Keyboard focus is in a Textbox or Button, the spaces are recorded in that control (i.e. if button, causes a click) and does not go through the PreviewTextInput in my Shell.xaml

I would like to do one of the following

  1. how to make the Shell preview those characters instead of the control with focus
  2. once I get StartOfTransmission, move focus to my Shell, then after EndOfTransmission, set focus back to the control
  3. Or some other way I am unaware of


I have found something that works.. but I don’t Like it.

In my Shell.Xaml, I added the following..

<TextBlock x:Name="lblBarcodeFocus" Focusable="True" Width="0" Height="0" />

Then in my processing code, if I detect a “StartOfTransmission”, I set focus to this control, (remembering what had focus), then after I am done processing, I set focus back to the original control.

There has to be a more “elegant” way of doing this… but I can move on to other issues 🙂

Answered By – adondero

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *