VB.net getasynckeystate只有一次日志密钥

时间:2015-03-06 03:46:15

标签: vb.net

让我们从我的代码开始:

Private Sub tmrKeys_Tick(sender As Object, e As EventArgs) Handles tmrKeys.Tick
    Dim i As Integer
    For i = 8 To 90
        If GetAsyncKeyState(i) < 0 AndAlso GlobalVariables.pressed(i) = False Then
            GlobalVariables.pressed(i) = True
            GlobalVariables.charValue = Chr(i)
        Else
            GlobalVariables.pressed(i) = False
        End If
    Next i

    If GlobalVariables.charValue <> Nothing Then
        If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
            txtLogs.Text &= GlobalVariables.charValue
        Else
            txtLogs.Text &= GlobalVariables.charValue.ToLower
        End If
        GlobalVariables.txtLength += 1
        GlobalVariables.charValue = Nothing
    End If
End Sub

基本上它有效,有点像。键被记录到文本框,但如果我按住一个例子,我得到多个a。

为了阻止这种情况,我添加了globalvariablespressed(i)。按下后,它将设置为true。它现在不应该记录该键,直到它被释放,重置为按下为false。但是它一直在记录,我不知道为什么。

1 个答案:

答案 0 :(得分:0)

你的循环应该是:

For i = 8 To 90
    If GetAsyncKeyState(i) < 0 Then
        If GlobalVariables.pressed(i) = False  Then
            GlobalVariables.pressed(i) = True
            GlobalVariables.charValue = Chr(i)
        End If
    Else
        GlobalVariables.pressed(i) = False
    End If
Next i
...
...