在Form上添加兩個Timer控件,然後在代碼窗口中復制粘貼以下代碼:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 100
Timer2.Interval = 500
Timer1.Enabled = False
End Sub
Private Function MyHotKey(vKeyCode) As Boolean
MyHotKey = GetAsyncKeyState(vKeyCode) < 0
End Function
Private Sub Timer1_Timer()
SendKeys "{5}"
SendKeys "{3}"
SendKeys "{2}"
SendKeys "{4}"
SendKeys "{7}"
SendKeys "{8}"
SendKeys "{9}"
End Sub
Private Sub Timer2_Timer()
If MyHotKey(vbKey6) Then
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End If
End Sub
程序運行後,按下鍵盤字符6鍵後開始不停按鍵操作,再次按下字符6鍵即停止。這段代碼只適用於目標程序處於前臺窗口激活狀態下使用!