title: Capture keyboard inputs without a textbox
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Here's my code in case if anyone's interested: <a href="http://pastebin.com/q6AkghvN" rel="nofollow">http://pastebin.com/q6AkghvN</a></p>
  
  <p>Just a note, I followed the credit card swipe guide from 
  <a href="http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing" rel="nofollow">http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing</a> and modified it slightly for my usecase.</p>
  
  <p>--- EDITED ---</p>
  
  <p>Thanks Paul and everyone else for their help!</p>
  
  <p>Here is my solution if anyone is interested:</p>
  
  <pre><code>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('^') &amp;&amp; buffer.Contains(';') &amp;&amp; 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;
          }
      }
  </code></pre>