我试图抓住刷卡的输入,但我被卡住了使用C#Windows窗体 [英] I'm trying to catch the input of the swiped card but I'm stuck using C# Windows Forms

查看:151
本文介绍了我试图抓住刷卡的输入,但我被卡住了使用C#Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用测试卡,这是我刷卡之后的输出,可以确定

I am using a test card and this is the output after I swiped the card and it is ok

但是当我试图通过提示将其刷新到messagebox这将是输出

But when I'm trying to get the data of the swiped through prompting it to messagebox this will be the output

我该如何解决?我期望输出与第一张图像相同,也将是消息框的消息

How can I fix this? I am expecting the output same as the first image, and it will also be the message of the messagebox

这是我的代码:

private void CreditCardProcessor_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
            KeyPress += CreditCardProcessor_KeyPress;
        }
    private bool inputToLabel = true;
        private void CreditCardProcessor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (inputToLabel)
            {
                label13.Text = label13.Text + e.KeyChar;
                e.Handled = true;

            }
            else
            {
                e.Handled = false;
            }

            MessageBox.Show(label13.Text);
        }

简而言之,我想在刷卡后运行一个功能,并使用它数据在我的功能中使用。 :)

In short I want to run a function after swiping the card, and use its data to be use in my function. :)

推荐答案

您需要更具体地解决您的问题。从您的卡片扫描器通过键盘缓冲区操作的外观。 (许多卡片扫描仪以这种方式操作)这意味着条形码的每个字符都被接收为一个字符,这就是为什么你可以捕获这个 OnKeyPress

You'll need to be more specific with your question. From the looks of things your card scanner is operating through the keyboard buffer. (Many card scanners operate this way) This means that every character of the strip is received as a character which is why you can capture this OnKeyPress.

如果您想知道为什么一次只能看到一个字符,那正是因为您收到每个字符的提示框。如果您想知道什么时候可以使用该代码使用整个卡片信息来调用功能,您需要的是:

If you're wondering why you're only seeing one character at a time it is exactly because you're raising a message box with each character received. If you want to know when you can call a function with the whole card info using that code what you'll need is something like:

private bool inputToLabel = true;
private StringBuilder cardData = new StringBuilder();
private void CreditCardProcessor_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!inputToLabel)
            return;

        if (e.KeyChar == '\r')
        {
            MessageBox.Show(cardData.ToString()); // Call your method here.
        }
        else
        {
            cardData.Append(e.KeyChar);
            //label13.Text = label13.Text + e.KeyChar;
        }
        e.Handled = true;
    }

注意事项:这是假设读卡器库配置为终止卡阅读带回车。 (\r)您需要阅读或尝试使用它进行设置,以确定是否可以/发送终止字符以知道读卡完成。否则您可以观察输出字符串的模式。 (即,当捕获的字符串以??结束时)尽管这不太优化。

Caveat: This is assuming that the card reader library is configured to terminate a card read with carriage return. (\r) You'll need to read up, or experiment with it for settings as to whether it can/does send a terminating character to know when the card read is complete. Failing that you can watch the output string for patterns. (I.e. when the captured string ends with "??") Though this is less optimal.

这篇关于我试图抓住刷卡的输入,但我被卡住了使用C#Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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