Capture keyboard inputs without a textbox

2019-02-15 14:46发布

问题:

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.

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.

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?

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

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

--- EDITED ---

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

回答1:

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.

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

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.