我在使用这个API时出现头痛的问题GetAsyncKeyState
以下是代码段:
private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
private sub timer1_timer()
If CBool(GetAsyncKeyState(vbKeyF5)) = True Then    StartLogo = True
    Text6.Text = ""
End If
If StartLogo = True Then
   If CBool(GetAsyncKeyState(vbKey1)) = True Then Text6.Text = Text6.Text & "1"
    If CBool(GetAsyncKeyState(vbKey2)) = True Then Text6.Text = Text6.Text & "2"
    If CBool(GetAsyncKeyState(vbKey3)) = True Then Text6.Text = Text6.Text & "3"
    If CBool(GetAsyncKeyState(vbKey4)) = True Then Text6.Text = Text6.Text & "4"
    If CBool(GetAsyncKeyState(vbKey5)) = True Then Text6.Text = Text6.Text & "5"
    If CBool(GetAsyncKeyState(vbKey6)) = True Then Text6.Text = Text6.Text & "6"
    If CBool(GetAsyncKeyState(vbKey7)) = True Then Text6.Text = Text6.Text & "7"
    If CBool(GetAsyncKeyState(vbKey8)) = True Then Text6.Text = Text6.Text & "8"
    If CBool(GetAsyncKeyState(vbKey9)) = True Then Text6.Text = Text6.Text & "9"
    If CBool(GetAsyncKeyState(vbKey0)) = True Then Text6.Text = Text6.Text & "0"
    If CBool(GetAsyncKeyState(vbKeyNumpad1)) = True Then Text6.Text = Text6.Text & "1"
    If CBool(GetAsyncKeyState(vbKeyNumpad2)) = True Then Text6.Text = Text6.Text & "2"
    If CBool(GetAsyncKeyState(vbKeyNumpad3)) = True Then Text6.Text = Text6.Text & "3"
    If CBool(GetAsyncKeyState(vbKeyNumpad4)) = True Then Text6.Text = Text6.Text & "4"
    If CBool(GetAsyncKeyState(vbKeyNumpad5)) = True Then Text6.Text = Text6.Text & "5"
    If CBool(GetAsyncKeyState(vbKeyNumpad6)) = True Then Text6.Text = Text6.Text & "6"
    If CBool(GetAsyncKeyState(vbKeyNumpad7)) = True Then Text6.Text = Text6.Text & "7"
    If CBool(GetAsyncKeyState(vbKeyNumpad8)) = True Then Text6.Text = Text6.Text & "8"
    If CBool(GetAsyncKeyState(vbKeyNumpad9)) = True Then Text6.Text = Text6.Text & "9"
    If CBool(GetAsyncKeyState(vbKeyNumpad0)) = True Then Text6.Text = Text6.Text & "0"End If
end sub但是在这当运行时。按下F5时text6中会出现在按F5之前按过的数字,而且顺序也不是正确的。比如:
在按下F5之前我按一些数字:1、2、5、4
当按下F5时text6中就出现了:1245或2154等顺序不对的数字。求助:当按下F5时text6中不会出现任何以前按过的数字。只有在按下F5后所做的数字输入才出现在Text6中。

解决方案 »

  1.   

    把判断键状态的代码改下
    If GetAsyncKeyState(vbKey1) < 0 Then Text6.Text = Text6.Text & "1"
    If GetAsyncKeyState(vbKey2) < 0 Then Text6.Text = Text6.Text & "2"
    If GetAsyncKeyState(vbKey3) < 0 Then Text6.Text = Text6.Text & "3"
    If GetAsyncKeyState(vbKey4) < 0 Then Text6.Text = Text6.Text & "4"
    If GetAsyncKeyState(vbKey5) < 0 Then Text6.Text = Text6.Text & "5"
    If GetAsyncKeyState(vbKey6) < 0 Then Text6.Text = Text6.Text & "6"
    If GetAsyncKeyState(vbKey7) < 0 Then Text6.Text = Text6.Text & "7"
    If GetAsyncKeyState(vbKey8) < 0 Then Text6.Text = Text6.Text & "8"
    If GetAsyncKeyState(vbKey9) < 0 Then Text6.Text = Text6.Text & "9"
    If GetAsyncKeyState(vbKey0) < 0 Then Text6.Text = Text6.Text & "0"
    If GetAsyncKeyState(vbKeyNumpad1) < 0 Then Text6.Text = Text6.Text & "1"
    If GetAsyncKeyState(vbKeyNumpad2) < 0 Then Text6.Text = Text6.Text & "2"
    If GetAsyncKeyState(vbKeyNumpad3) < 0 Then Text6.Text = Text6.Text & "3"
    If GetAsyncKeyState(vbKeyNumpad4) < 0 Then Text6.Text = Text6.Text & "4"
    If GetAsyncKeyState(vbKeyNumpad5) < 0 Then Text6.Text = Text6.Text & "5"
    If GetAsyncKeyState(vbKeyNumpad6) < 0 Then Text6.Text = Text6.Text & "6"
    If GetAsyncKeyState(vbKeyNumpad7) < 0 Then Text6.Text = Text6.Text & "7"
    If GetAsyncKeyState(vbKeyNumpad8) < 0 Then Text6.Text = Text6.Text & "8"
    If GetAsyncKeyState(vbKeyNumpad9) < 0 Then Text6.Text = Text6.Text & "9"
    If GetAsyncKeyState(vbKeyNumpad0) < 0 Then Text6.Text = Text6.Text & "0"
      

  2.   

    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private StartLogo As BooleanPrivate Sub timer1_timer()
    If GetAsyncKeyState(vbKeyF5) < 0 Then
        StartLogo = True
        Text6.Text = ""
    End If
    If StartLogo = True Then
        If GetAsyncKeyState(vbKey1) < 0 Then Text6.Text = Text6.Text & "1"
        If GetAsyncKeyState(vbKey2) < 0 Then Text6.Text = Text6.Text & "2"
        If GetAsyncKeyState(vbKey3) < 0 Then Text6.Text = Text6.Text & "3"
        If GetAsyncKeyState(vbKey4) < 0 Then Text6.Text = Text6.Text & "4"
        If GetAsyncKeyState(vbKey5) < 0 Then Text6.Text = Text6.Text & "5"
        If GetAsyncKeyState(vbKey6) < 0 Then Text6.Text = Text6.Text & "6"
        If GetAsyncKeyState(vbKey7) < 0 Then Text6.Text = Text6.Text & "7"
        If GetAsyncKeyState(vbKey8) < 0 Then Text6.Text = Text6.Text & "8"
        If GetAsyncKeyState(vbKey9) < 0 Then Text6.Text = Text6.Text & "9"
        If GetAsyncKeyState(vbKey0) < 0 Then Text6.Text = Text6.Text & "0"
        If GetAsyncKeyState(vbKeyNumpad1) < 0 Then Text6.Text = Text6.Text & "1"
        If GetAsyncKeyState(vbKeyNumpad2) < 0 Then Text6.Text = Text6.Text & "2"
        If GetAsyncKeyState(vbKeyNumpad3) < 0 Then Text6.Text = Text6.Text & "3"
        If GetAsyncKeyState(vbKeyNumpad4) < 0 Then Text6.Text = Text6.Text & "4"
        If GetAsyncKeyState(vbKeyNumpad5) < 0 Then Text6.Text = Text6.Text & "5"
        If GetAsyncKeyState(vbKeyNumpad6) < 0 Then Text6.Text = Text6.Text & "6"
        If GetAsyncKeyState(vbKeyNumpad7) < 0 Then Text6.Text = Text6.Text & "7"
        If GetAsyncKeyState(vbKeyNumpad8) < 0 Then Text6.Text = Text6.Text & "8"
        If GetAsyncKeyState(vbKeyNumpad9) < 0 Then Text6.Text = Text6.Text & "9"
        If GetAsyncKeyState(vbKeyNumpad0) < 0 Then Text6.Text = Text6.Text & "0"
    End IfEnd Sub
      

  3.   

    MyKey = GetAsyncKeyState(vbKeyLButton)
    If (MyKey And &H1) = &H1 Then 
        msgbox "按下鼠标"
    End If
    按这样的格式试试
      

  4.   

    楼上的正解.另外,建议楼主改写一下代码.Private Sub Timer1_Timer()
            Dim i As Long
            For i = vbKeyA To vbKeyZ
                    If GetAsyncKeyState(i) <> 0 Then
                            Me.Caption = i
                    End If
            Next i
    End Sub这样是不是更简洁?   :)
      

  5.   

    呵呵...去看我BLOG上泰坦之旅那个修改器,里面有这个函数的调用方法,不是用TIMER,会漏的,是用一个回调