VB写个连续按键程序

VB写个连续按键程序,第1张

在FORM1上放一个TEXT1和COMMAND1控件,添加窗体FORM2,在上面放TEXT2、COMMAND2和TIMER1 FORM1代码如下: Private Sub Command1_Click() '给FORM2上的TIMER1付值,每间隔XX时间执行一次。 Dim numb1 As Integer numb1 = Int(Text1.Text) Form2.Timer1.Interval = numb1 Form2.Show End Sub Private Sub Form_Load() '程序载入时TIMER1不工作 Form2.Timer1.Enabled = False End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) '设置TEXT1只能输入数字,0到9字符的ASC码是十进制的48到57,设置ASC码不等于8是为了允许退格键 If (KeyAscii <48 Or KeyAscii >57) And KeyAscii <>8 Then KeyAscii = 0 End Sub FORM2窗体代码 Dim str1 As String '定义变量,取TEXT2的值,即你输入的内容 Private Sub Command2_Click() str1 = Text2.Text Text2.Text = "" End Sub Private Sub Command2_KeyDown(KeyCode As Integer, Shift As Integer) '当按下A时,TIMER1激活 If KeyCode = vbKeyA Then Timer1.Enabled = True End If End Sub Private Sub Command2_KeyUp(KeyCode As Integer, Shift As Integer) '松开a时TIMER1停止 If KeyCode = vbKeyA Then Timer1.Enabled = False End If End Sub Private Sub Timer1_Timer() 'text2输出 Text2.Text = Text2.Text &str1 End Sub

写了一个简易代码,基本上能满足你的需求~~

在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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11604705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存