的KeyDown键转换为一个字符串中的C# [英] Convert KeyDown keys to one string C#

查看:646
本文介绍了的KeyDown键转换为一个字符串中的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有磁卡读写器,它模拟键盘打字时,用户刷卡卡。我需要处理这个键盘打字一个字符串,当我的WPF窗口被聚焦。我能得到这个类型的重点名单,但我不知道如何将它们转换成一串。

I have magnetic card reader, It emulates Keyboard typing when user swipes card. I need to handle this keyboard typing to one string, when my WPF window is Focused. I can get this typed Key list, but I don't know how to convert them to one string.

private void Window_KeyDown(object sender, KeyEventArgs e)
{
   list.Add(e.Key);
}

修改:简单的ToString()方法,不是帮助。我已经尝试过这一点。

EDIT: Simple .ToString() method not helps. I've tried this already.

推荐答案

而不是增加一个列表为什么不建立字符串:

Rather than adding to a list why not build up the string:

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

显然,你需要重新设置输入的String.Empty 当你开始下一个事务。

Obviously you need to reset input to string.Empty when you start the next transaction.

这篇关于的KeyDown键转换为一个字符串中的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆