如何为 vb.net 中的每个字符制作快捷方式



有一个RichTextBox,如果用户输入任何键,它将显示在那里。我尝试了"."和"-"等的 GetAsyncKeyState,但它不起作用,但是 A-Z 和 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

我希望当用户在键盘中按十进制富文本框显示添加"."到文本等

提前感谢您的任何帮助

以下是连字符和句点的正确键码,请将它们添加到您的 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 = ">"

最新更新