捕获没有文本框的键盘输入 [英] Capture keyboard inputs without a textbox

查看:119
本文介绍了捕获没有文本框的键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它从健康卡中读取数据并解析它们以获取基本信息,例如D.O.B.,健康卡号和姓名.现在,我有一个文本框可以从刷卡机输入信息,并且效果很好,但是我觉得可以有一个更好的方法.

I have a application that reads data from health cards and parse them for basic info like D.O.B., Health Card #, and names. Right now, I have a textbox that takes input from the card swiper and it works great, but I feel there could be a better approach for this.

我希望在应用程序的后台有一个键盘侦听器,该侦听器可捕获来自刷卡机的输入并解析数据,而无需文本框.我想我将在Form1_Load中需要一个循环函数,该函数主动侦听键盘输入,为输入准备缓冲区,然后在检测到回车时继续解析缓冲的数据.解析完成后,清除缓冲区.

I want to have a keyboard listener in the background of the application that captures input from the card swiper and parse the data without the need of a textbox. I figure I'll need a loop function in the Form1_Load that actively listens for keyboard inputs, prepare a buffer for the input, and then when a carriage return is detected, go ahead and parse the buffered data. When the parsing is done, clear the buffer.

我的问题是我对C#还是比较陌生,我不知道在没有文本框的情况下应该使用什么来监听键盘输入.有人可以指出我正确的方向吗?

My problem is I'm relatively new to C# and I don't know what I should use for listening to keyboard inputs without a textbox. Could someone point me in the right direction?

如果有人感兴趣,这是我的代码: http://pastebin.com/q6AkghvN

Here's my code in case if anyone's interested: http://pastebin.com/q6AkghvN

仅需注意,我遵循了信用卡刷卡指南 http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing 并针对我的用例进行了一些修改.

Just a note, I followed the credit card swipe guide from http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing and modified it slightly for my usecase.

-编辑--

感谢保罗和其他所有人的帮助!

Thanks Paul and everyone else for their help!

如果有人感兴趣,这是我的解决方案:

Here is my solution if anyone is interested:

private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
    {
        lblStatus.Text = "Reading Card...";
        lblStatus.ForeColor = Color.Blue;
        if (e.KeyChar != (char)Keys.Enter)
        {
            buffer += e.KeyChar;
        }
        else
        {
            lblStatus.Text = "Parsing Card...";
            if (buffer.Contains('^') && buffer.Contains(';') && buffer.Contains('='))
            {
                try
                {
                    string[] cardData = buffer.Split(';');
                    string[] caretData = cardData[0].Split('^');
                    string[] nameData = caretData[1].Split('/');
                    string[] equalData = cardData[1].Split('=');
                    tBoxHealthCardNumber.Text = equalData[0];
                    tBoxDateOfBirth.Text = FormatBirthday(equalData[1]);
                    tBoxFirstName.Text = TrimName(nameData[1]);
                    tBoxLastName.Text = TrimName(nameData[0]);
                    tBoxDateTimeScanned.Text = DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm");
                    e.Handled = true;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                lblStatus.Text = "Error Reading Card";
            }

            buffer = "";
            lblStatus.Text = "Ready";
            lblStatus.ForeColor = Color.Green;
        }
    }

推荐答案

如果将键处理程序添加到表单中,则当焦点位于控件(例如控件)上时,将不会看到按键.一个文本框.为了使表单即使在有重点控制的情况下也能看到按键,还必须启用

If you add a key handler to the form you will not see the key presses when focus is on a control, e.g. a textbox. For the form to see the key presses even when there is a focused control, you must also enable the KeyPreview property.

然后,当您希望接收这些事件时,可以在表单上为KeyDownKeyPress和/或KeyUp添加处理程序.

You can then add a handler for KeyDown, KeyPress and/or KeyUp on the form as you desire to receive these events.

正如您在文档中阅读的KeyPreview一样,如果将Handled属性设置为true,则可以防止随后将事件发送给焦点控件,即可以隐藏某些关键事件通过集中控制.

As you can read in the documentation to KeyPreview, if you set the Handled property to true, you can prevent the event from being subsequently sent to the focused control, i.e. you can hide certain key events from being seen by the focused control.

这篇关于捕获没有文本框的键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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