在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键即停止。这段代码只适用于目标程序处于前台窗口激活状态下使用!
'如果用Msgbox来显示事件发生,keypress事件会被忽略Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Print "KeyDown事件发生了,KeyCode的值是:" &KeyCode &" Shift参数的值是:" &Shift
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Print "KeyPress事件发生了,KeyAscii的值是:" &KeyAscii
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)