1

I can't seem to find a way to catch the input of a magnetic card reader. When it swipes, the input gets into active text editor, like say a notepad.

Unfortunately, the focus on textbox field won't do the trick, because I'm required to make it a label instead of a textbox. Thus, I need a way to catch the input from the USB device to a variable or label instead.

Does anyone knows of a .NET class I could use to do this or any better ideas?

3
  • Can't you use the textbox and format it to look like a label? After the input ends, you could make it readonly. Commented Mar 24, 2011 at 1:05
  • yes it is windows forms, users are not allowed to input manually. so I can't use a textbox Commented Mar 24, 2011 at 1:13
  • You need to figure out how text gets to notepad by spying on windows events (i.e. use Spy++ from VS tools) and then acting accordingly. Likley Bala R's solution with KeyPress is the right one (drag'n'drop or copy-paste are less likley options, setting text directly with WM_SETTEXT event is another possiblilty). Note that your question is unrelated to USB - removing tag may be good option. Commented Mar 24, 2011 at 1:24

2 Answers 2

4

If it's a winforms app you could do

 private void Form1_Load(object sender, EventArgs e) { KeyPreview = true; KeyPress += Form1_KeyPress; } private bool inputToLabel = true; void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (inputToLabel) { label1.Text = label1.Text + e.KeyChar; e.Handled = true; } else { e.Handled = false; } } 

and as long as the window has focus, the keypress characters will go to the label's text.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 as it is a likley way the program that handles this device pastes text to the editor.
1

I don't think there will be anyway for you to prevent the user from manual input. I suspect the card reader that you have emulates a keyboard. So, to be able to read from the reader, you must receive keyboard input, and keyboard input means the user can type anything they like.

A possible solution is to change your card reader to one that uses an API to read from cards.

If getting a better card reader isn't an option, I think the best method to do this is to have a button. When the button is clicked, open a new form that contains the code @Bala R provided. But in addition, close the form within 1 second from the first key input. This will prevent users from tampering the input manually, but will provide sufficient time for the reader to complete.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.