用vb做全局鼠标键盘钩子.求助
1个回答
展开全部
Option Explicit
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Sub CopyMemory_M Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public Win_Max_Fg As Long
Public M_Msg_Type As Long
Public M_Move_Type As Long
Type Msg7K_Info
Msg_Type As Long
i As Long
Do_Fg As Long
End Type
Public Msg7K_Info As Msg7K_Info
Public Type MouseMsgs
x As Long 'x座标
y As Long 'y座标
a As Long
b As Long
time As Long 'Window运行时间
End Type
Public Const WH_MOUSE_LL = 14
'-----------------------------------------
'消息
Public Const HC_ACTION = 0
'鼠标消息
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201 '左键按下
Public Const WM_LBUTTONUP = &H202 '左键松开
Public Const WM_LBUTTONDBLCLK = &H203 '左键双击
Public Const WM_RBUTTONDOWN = &H204 '右键按下
Public Const WM_RBUTTONUP = &H205 '右键松开
Public Const WM_RBUTTONDBLCLK = &H206 '右键双击
Public Const WM_MBUTTONDOWN = &H207 '中键按下
Public Const WM_MBUTTONUP = &H208 '中键松开
Public Const WM_MBUTTONDBLCLK = &H209 '中键双击
Public Const WM_MOUSEACTIVATE = &H21 '当光标在某个非激活的窗口中而用户正按着鼠标的某个键发送此消息给当前窗口
Public Const WM_MOUSEFIRST = &H200
Public Const WM_MOUSELAST = &H209
Public Const WM_MOUSEWHEEL = &H20A '中键滚动
Public MouseMsg As MouseMsgs
Public lHook As Long '记录Hook的值,以便退出程序的时候销毁Hook
lHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf CallMouseHookProc, App.hInstance, 0)
'鼠标钩子
Public Function CallMouseHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If code <> 0 Then
CallMouseHookProc = CallNextHookEx(0, code, wParam, lParam) '使用CallNextHookEx,来保证鼠标钩子能够被其它程序使用
End If
If code = HC_ACTION Then
CopyMemory_M MouseMsg, lParam, LenB(MouseMsg)
......
处理代码
end if
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Sub CopyMemory_M Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public Win_Max_Fg As Long
Public M_Msg_Type As Long
Public M_Move_Type As Long
Type Msg7K_Info
Msg_Type As Long
i As Long
Do_Fg As Long
End Type
Public Msg7K_Info As Msg7K_Info
Public Type MouseMsgs
x As Long 'x座标
y As Long 'y座标
a As Long
b As Long
time As Long 'Window运行时间
End Type
Public Const WH_MOUSE_LL = 14
'-----------------------------------------
'消息
Public Const HC_ACTION = 0
'鼠标消息
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201 '左键按下
Public Const WM_LBUTTONUP = &H202 '左键松开
Public Const WM_LBUTTONDBLCLK = &H203 '左键双击
Public Const WM_RBUTTONDOWN = &H204 '右键按下
Public Const WM_RBUTTONUP = &H205 '右键松开
Public Const WM_RBUTTONDBLCLK = &H206 '右键双击
Public Const WM_MBUTTONDOWN = &H207 '中键按下
Public Const WM_MBUTTONUP = &H208 '中键松开
Public Const WM_MBUTTONDBLCLK = &H209 '中键双击
Public Const WM_MOUSEACTIVATE = &H21 '当光标在某个非激活的窗口中而用户正按着鼠标的某个键发送此消息给当前窗口
Public Const WM_MOUSEFIRST = &H200
Public Const WM_MOUSELAST = &H209
Public Const WM_MOUSEWHEEL = &H20A '中键滚动
Public MouseMsg As MouseMsgs
Public lHook As Long '记录Hook的值,以便退出程序的时候销毁Hook
lHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf CallMouseHookProc, App.hInstance, 0)
'鼠标钩子
Public Function CallMouseHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If code <> 0 Then
CallMouseHookProc = CallNextHookEx(0, code, wParam, lParam) '使用CallNextHookEx,来保证鼠标钩子能够被其它程序使用
End If
If code = HC_ACTION Then
CopyMemory_M MouseMsg, lParam, LenB(MouseMsg)
......
处理代码
end if
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询