Click here to Skip to main content
15,921,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a usb mifare card reader, which when presenting a card acts as a keyboard and spits out a 10 digit number.
So say I have a text box as the default selected control a user puts the card on the reader and the number is read into the textbox
What I'm trying to do is have a hidden control that fires off an event once the 10 digits are read.
Weird thing is I've had this working once!

What I have tried:

This just waits for more input :-


VB
Private Sub TextBox1_Leave(sender As System.Object, e As System.EventArgs) Handles TextBox1.Leave

        msgbox(textbox1.text.tostring)

End Sub

'This just reads the first character

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    MsgBox(TextBox1.Text.ToString)
    


End Sub
Posted
Updated 27-Sep-17 2:41am

Think I may have solved it with a timer - does this look okay ? I'd prefer it to be hidden though, currently there's a text box with a flashing cursor

VB
Private Sub rfidbox_textchanged(sender As System.Object, e As System.EventArgs) Handles rfidbox.TextChanged

        Timer1.Stop()
        Timer1.Start()

End Sub


Tick event

VB
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

    Timer1.Stop()
    cardnumber = rfidbox.Text.ToString

    Dim name As String
    Dim x As New signclass

    If rfidbox.Text = "" Then

    Else
        name = x.getnamefromcard(rfidbox.Text.ToString)


        MsgBox(name)

        rfidbox.Text = ""

    End If
 
Share this answer
 
Comments
Dave Kreskowiak 26-Sep-17 8:22am    
The timer is a bad idea. TextChanged fires on every keystroke in the box, so you're going to stop and start the timer ten times for every card.

Is the card reader configurable? Does if have the ability to send prefix and postfix characters with the code? If so, your job gets easier.

If not, you have a problem. Keyboard wedge devices can be a little finicky. As the code is being "typed" by the reader, what if a system dialog pops up? Part of the code is going to the dialog and not your textbox. The only way to RELIABLY say "this is the entire number" is to click a button.

A non-visible textbox doesn't get the focus, so it also doesn't get any keyboard input. Also, what if the reader fails to read the card? How else are you going to get the ID number?

If you can do pre and post fix codes, it gets a bit more complicated but much more reliable. Using Form.KeyPreview you can look for the prefix code grab the keystrokes for the code until you see the postfix codes.
caffrey_1 26-Sep-17 9:46am    
The card reader is dumb, it acts just like a keyboard and throws the code into whatever window \ control is in focus. I don't have to worry about system dialogs and things as it's a standalone app
(It's a sign in system) So on the main screen there is basically Sign In and Sign out buttons. For convenience I planned to add card reading functions to the main form.
I've got it working reasonably reliably with the above code as in it's returning the correct name pulled from a LDAP field but I don't like the text box being seen (I got it down to a 1x1 pixel size but it still flashes away to itself!)

Starting to think this is a bad idea for the time it saves the end user so may just abandon it
Think I fixed it with this thanks to your pointer (The USB reader enters after input)

VB
Private Sub rfidinput_keypress(sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rfidinput.KeyPress

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then

        Dim name As String
        Dim x As New signclass

        If rfidinput.Text = "" Then

        Else

            name = x.getnamefromcard(rfidinput.Text.ToString)

            MsgBox(name)

            rfidinput.Text = ""

        End If

    End If


End Sub


I maintain focus on the textbox by refocusing when other elements are pressed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900