如何为vb.net中的每个字符创建快捷方式?

时间:2013-11-11 10:45:30

标签: 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

我想当用户在键盘上按十进制时RichTextBox显示添加“。”到文本等等

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:0)

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

If key = "-" AndAlso My.Computer.Keyboard.ShiftKeyDown Then key = "_"
If key = "." AndAlso My.Computer.Keyboard.ShiftKeyDown Then key = ">"

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

{{1}}