用vb做全局鼠标键盘钩子.求助

 我来答
百度网友34bbf6a
2017-01-05 · TA获得超过111个赞
知道小有建树答主
回答量:214
采纳率:100%
帮助的人:99.2万
展开全部
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式