如何在按键事件中使退格键正常工作?

时间:2013-08-17 12:09:00

标签: vb.net backspace

我的代码在按键事件中,当我按下Backspace按钮时,它会显示一些特殊字符(如squre框)。如何防止这种情况并使退格键正常工作?请帮忙。

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

1 个答案:

答案 0 :(得分:1)

我有点想知道这个代码有什么样的用法,除了系统范围的键盘记录器,无论如何回答你的问题,这会做的伎俩,但它不是万无一失的(就像它无法检测选定的文本并删除它)

    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