4

我有一个文本框,当输入的键是“返回”时,它有 keydown 事件字母“a”被覆盖变成“b”,但不会变成“ab”。有谁知道这是什么原因?

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

在“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

2 回答 2

4

KeyDown 事件旨在让您知道哪些键同时按下,并且您的条形码阅读器似乎模拟键盘,因此您需要连接它发送的字符

在您的 Key_Down 事件中,您必须执行以下操作:

this.scannedString += e.Key;

当你看到返回时:

barcodeTextBox.Text = this.scannedString;

于 2013-01-28T13:25:51.313 回答
2

不确定我是否理解您的问题,但我认为这是您的解决方案:

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();
        }
    }

我已经用条形码扫描仪对其进行了测试,效果很好。

于 2013-01-28T14:04:06.457 回答