Key down allows only one character event in a WPF text field

I have a text box that has a keydown event when the key entered is "return". I have a barcode reader that reads the text in it, but it does not write more than one key, i.e. only one letter is written, say "a", and if I write the second letter "a", it is rewritten to become "b", but does not become "ab". Does anyone know what is causing this?

private void barcodetexbox_KeyDown(object sender, KeyEventArgs e) { if (scannedString.Text != "" && e.Key==Key.Return) { //do something } } 

and in "MainWindow.xaml"

 <TextBox x:Name="scannedString" HorizontalAlignment="Left" Height="50" Margin="468,164,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="450" FontSize="24" Focusable="True" Padding="0,6,0,0" KeyDown="barcodetexbox_KeyDown" /> 
+4
source share
2 answers

The KeyDown event is designed to let you know which keys are currently unavailable, and your barcode reader seems to mimic the keyboard, so you'll need to combine the characters it sends.

in your Key_Down event, you will need to do something like this:

this.scannedString += e.Key;

and when you see return:

barcodeTextBox.Text = this.scannedString;

+4
source

Not sure I understood your problem, but I think this is your solution:

 private void scannedString_PreviewKeyDown(object sender, KeyEventArgs e) { if ((sender as TextBox).Text !="" && e.Key == Key.Return) { MessageBox.Show((sender as TextBox).Text); // I mean do some thing (sender as TextBox).Clear(); } } 

I tested it with a barcode scanner and it works well.

+2
source

All Articles