Option ExplicitPrivate Const VK_LBUTTON = &H1Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
      
Private Sub Form_KeyPress(KeyAscii As Integer)
  If GetAsyncKeyState(vbKeyF11) <> 0 Then
     MsgBox "你按了F11!"
  End If
  
  If GetAsyncKeyState(VK_LBUTTON) <> 0 Then
     MsgBox "你按了左键!"
  End If
  
  If GetAsyncKeyState(vbKeyA) <> 0 Then
     MsgBox "你按了a!"
  End If
    
  If GetAsyncKeyState(vbKeyB) <> 0 Then
     MsgBox "你按了b!"
  End If
End Sub
1\为什么单击左键和按F11键不立即响应,而按a和b键却会响应?2\能实现按了左键和F11键立即响应的代码吗

解决方案 »

  1.   

    VK_LBUTTON 是鼠标左键。
    键盘左键是 vbKeyLeft把代码放在计时器中:Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
          
    Private Sub Timer1_Timer()
      If GetAsyncKeyState(vbKeyF11) <> 0 Then
         MsgBox "你按了F11!"
      End If
      
      If GetAsyncKeyState(vbKeyLeft) <> 0 Then
         MsgBox "你按了左键!"
      End If
      
      If GetAsyncKeyState(vbKeyA) <> 0 Then
         MsgBox "你按了a!"
      End If
        
      If GetAsyncKeyState(vbKeyB) <> 0 Then
         MsgBox "你按了b!"
      End If
    End Sub