0

有一个 RichTextBox,如果用户输入任何键,它将显示在那里。我为“。”尝试了 GetAsyncKeyState。和“-”等但它不起作用但是 AZ 和 0-9 正常工作。

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 p As Boolean = CBool(GetAsyncKeyState(Keys.P))
    Dim i As Integer
    Dim dec As Boolean = CBool(GetAsyncKeyState(Keys.Decimal))
    Dim subtract As Boolean = CBool(GetAsyncKeyState(Keys.Subtract))
    Dim add As Boolean = CBool(GetAsyncKeyState(Keys.Add))



    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
            ElseIf key = vbBack Then
                If txtlogs.TextLength > 0 Then
                    txtlogs.Text = txtlogs.Text.Remove(txtlogs.TextLength - 1)
                End If
            ElseIf My.Computer.Keyboard.CtrlKeyDown Then
                txtlogs.Text &= " -[CTRL+" & key & "]"
            ElseIf subtract = True Then
                txtlogs.Text &= "-"
            ElseIf My.Computer.Keyboard.ShiftKeyDown AndAlso subtract = True Then
                txtlogs.Text &= "_"
            ElseIf dec = True Then
                txtlogs.Text &= "."
            Else
                txtlogs.Text &= key.ToLower
            End If
        End If

我想当用户在键盘 RichTextBox 中按小数点时显示添加“。” 到文字等等

感谢您提前提供任何帮助

4

1 回答 1

0

以下是连字符和句点的正确键码,将它们添加到您的 for 循环中:

If i = 189 Then key = "-"
If i = 190 Then key = "."

此外,要获得下划线和小于号,请在 for 循环后添加:

If key = "-" AndAlso My.Computer.Keyboard.ShiftKeyDown Then key = "_"
If key = "." AndAlso My.Computer.Keyboard.ShiftKeyDown Then key = ">"
于 2015-06-05T11:29:31.910 回答