我有磁卡读卡器,用户刷卡时模拟键盘输入.当我的WPF窗口是Focused时,我需要处理这个键盘输入到一个字符串.我可以得到这个键入的键列表,但我不知道如何将它们转换为一个字符串.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
list.Add(e.Key);
}
Run Code Online (Sandbox Code Playgroud)
编辑:简单.ToString()方法没有帮助.我已经尝试过了.
与其添加到列表中,不如不建立字符串:
private string input;
private bool shiftPressed;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
shiftPressed = true;
}
else
{
if (e.Key >= Key.D0 && e.Key <= Key.D9)
{
// Number keys pressed so need to so special processing
// also check if shift pressed
}
else
{
input += e.Key.ToString();
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
shiftPressed = false;
}
}
Run Code Online (Sandbox Code Playgroud)
显然input,string.Empty当您开始下一个事务时,您需要重置为。