下面代码按下F1则每3秒自动按下一次1键,按下F2则自动按键停止,按下F3程序退出。
不过,因为F1、F2还是系统的其它热键,所以,当你按下F1后,可能有帮助窗口弹出……
窗体代码:
Option Explicit
Private Sub Form_Load()
Form1.Visible = False
Timer1.Interval = 3000
Timer1.Enabled = False
HooK
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
Private Sub Timer1_Timer()
SendKeys "1"
End Sub
模块代码:
Option Explicit
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const VK_F1 = &H70
Private Const VK_F2 = &H71
Private Const VK_F3 = &H72
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Sub LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long)
Dim blnNum As Integer
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYUP, WM_SYSKEYUP
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = VK_F1 Then
blnNum = 1
ElseIf p.vkCode = VK_F2 Then
blnNum = 2
ElseIf p.vkCode = VK_F3 Then
blnNum = 3
Else
blnNum = 0
End If
Case Else
End Select
End If
If blnNum = 1 Then
Form1.Timer1.Enabled = True
ElseIf blnNum = 2 Then
Form1.Timer1.Enabled = False
ElseIf blnNum = 3 Then
UnHooK
End
Else
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End If
End Sub
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
更多VB代码可参阅我的博客:http://hi.baidu.com/zgmg/blog/item/8ea6aab18339234f092302db.html
需要找到句柄,给你个参考实例
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal Hwnd1 As Long, ByVal Hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
Shell "calc.exe", vbNormalNoFocus
End Sub
Private Sub Command1_Click()
Dim Hwnd1 As Long
Dim Hwnd2 As Long
Hwnd1 = FindWindow("SciCalc", "计算器")
Hwnd2 = FindWindowEx(Hwnd1, 0, "Button", "1")
SendMessage Hwnd2, WM_LBUTTONDOWN, ByVal 0&, ByVal 0&
SendMessage Hwnd2, WM_LBUTTONUP, ByVal 0&, ByVal 0&
End Sub
用按键精灵吧,现成的,不用自己写程序。