Public Class ucToken

    Dim possibleToken As String
    Event TokenScanned(ByRef token As String)


    Private Sub ucToken_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lblStatus.BackColor = Color.Gray
        RegisterKeyDown(Parent)
    End Sub

    Sub RegisterKeyDown(ByRef control As Control)
        AddHandler control.KeyDown, AddressOf FormLogin_KeyDown
        AddHandler control.Validating, AddressOf ucToken_Validating
    End Sub

    Private Sub FormLogin_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown, lblStatus.KeyDown
        Debug.Print(possibleToken)
        If e.KeyCode = Keys.Enter Then
            OnEnterKey()
        Else
            possibleToken += ChrW(e.KeyCode)
            lblStatus.BackColor = Color.Blue
        End If
    End Sub

    Private Sub ucToken_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
        OnEnterKey()
    End Sub

    Private Sub OnEnterKey()
        If possibleToken IsNot Nothing Then
            If possibleToken.Length >= BLUE_TOKEN_LENGTH Then
                lblStatus.BackColor = Color.YellowGreen
                RaiseEvent TokenScanned(possibleToken)
                possibleToken = ""
                Timer1.Interval = 500
                Timer1.Enabled = True
                Timer1.Start()
            End If
        End If
    End Sub

    Private Sub ucToken_Enter(sender As Object, e As EventArgs) Handles MyBase.Enter
        lblStatus.BackColor = Color.Yellow
    End Sub

    Private Sub ucToken_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
        lblStatus.BackColor = Color.Yellow
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Timer1.Enabled = False
            Timer1.Stop()
            lblStatus.BackColor = Color.Yellow
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub
End Class

