100分!vb模拟键盘全局钩子,要求当我按住W键的时候,等于每秒按下W键5次,要求全局!求高手支招!
都不是我要的。!求高手继续帮忙 展开
用按键精灵~~~
哈哈,肯定不行。
-----------------------------------
我再来回答下吧。我的想法就是 递归。也就是说,当产生按键这一事件时,让程序重复模拟5次此按钮,即重复执行这一事件5次。这是我在VB.NET中的代码:
Private i As Integer = 0
Private j As String
Private k As Object
Private t As KeyPressEventArgs
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
j = j & e.KeyChar.ToString
k = sender
t = e
i = i + 1
If i <= 5 Then
Form1_KeyPress(k, t)
Exit Sub
Else
MsgBox("你按下了:" & e.KeyChar & "键,当前系统自动重复5次按键后为:" & j)
i = 0
j = ""
Exit Sub
End If
End Sub
--------------------------------------------------
经测试是可行的,不过如果你具体用到你的程序中要做一定的修改。
最后的测试结果是:按一下a键:
只对W键做判断,不要长按W键,点按即可,02s一次模拟按钮,1s5次自己测试。
有些杀毒软件可能把钩子程序当做病毒来处理的,如果有这种情况关闭杀毒软件再测试。
我有的邮箱pt98@sina.com
'-----------------------------------
'纯浪费时间,给你的能看懂吗?还都不是你要的,以后把你要的嗮嗮我也学习一下。
一.在窗体上加入控件timer1,timer2,然后在窗体代码区复制下面代码:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = True
Timer2.Enabled = False
Timer2.Interval = 1000
End Sub
Private Sub Timer1_Timer()
If (GetAsyncKeyState(vbKeyW) And &H7FFF) <> 0 Then
Timer2.Enabled = False
SendKeys "{w 4}" '因为已按了1次w键,所以这里再按4次即可
Timer2.Enabled = True '之后每秒按5次w
End If
If (GetAsyncKeyState(vbKeyT) And &H7FFF) <> 0 Then '按t,则停止按键
Timer2.Enabled = False
End If
End Sub
Private Sub Timer2_Timer()
SendKeys "{w 5}"
End Sub
二.运行程序,Ok!
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim h As Variant, Y As Variant, nm As Variant
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim Locked As RECT
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
'Interval 为100 实时监控
Private Sub Timer2_Timer()
Dim i As Long
Dim j As Integer
Dim s As String
'循环判断按键
'只针对按键有效A~Z其他的控制键 和数子键没有写
For i = vbKeyA To vbKeyZ
If GetAsyncKeyState(i) <> 0 Then
s = Chr(i) '将按键的Ascii码 转换为对应的字母
For j = 0 To 3 '循环发送按键 4次(你按的时候已经有1次了加循环4就等于5次了)
SendKeys s '发送指定按键
Next j
End If
Next i
End Sub
'在vb6.0中测试成功
'不知道有没有达到你的要求 能够在一般的任何窗口 实现按键重复
'有的游戏可能可以用 但不能保证100%