使用全局挂钩拦截鼠标事件.阻止动作发生 [英] Intercepting mouse events using a global hook. Stop an action from happening

查看:191
本文介绍了使用全局挂钩拦截鼠标事件.阻止动作发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拦截和中断鼠标事件.可以说我想禁用鼠标右键按下事件,甚至鼠标移动事件.我还没弄清中断的部分.

I'm attempting to intercept and interrupt mouse events. Lets say I wanted to disable the right mouse button down event, or even the mouse move event. I haven't been able to figure out the interrupting part.

我正在使用以下代码(我假设使用非常广泛)来进行鼠标的全局挂钩.

I am using the (I assume pretty widely used) following code for Global Hooking of the mouse.

Private Structure MSLLHOOKSTRUCT
    Public pt As Point
    Public mouseData As Int32
    Public flags As Int32
    Public time As Int32
    Public extra As IntPtr
End Structure

Private _mouseHook As IntPtr
Private Const WH_MOUSE_LL As Int32 = 14

Private Delegate Function MouseHookDelegate(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
<MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As MouseHookDelegate
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As MouseHookDelegate, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr


Public Function HookMouse() As Boolean
    Debug.Print("Mouse Hooked")
    If _mouseHook = IntPtr.Zero Then
        _mouseProc = New MouseHookDelegate(AddressOf MouseHookProc)
        _mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
    End If
    Return _mouseHook <> IntPtr.Zero
End Function

Public Sub UnHookMouse()
    Debug.Print("Mouse UnHooked")
    If _mouseHook = IntPtr.Zero Then Return
    UnhookWindowsHookEx(_mouseHook)
    _mouseHook = IntPtr.Zero
End Sub

Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    'Debug.Print("Message = {0}, x={1}, y={2}", wParam.ToInt32, lParam.pt.X, lParam.pt.Y)
    If wParam.ToInt32 = 513 Then
        '''interrupt the left mouse button event here, but don't know what to return to do so.
    End If
    Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
End Function

推荐答案

返回1-很抱歉张贴此消息.

Return 1 - sorry for posting this.

这篇关于使用全局挂钩拦截鼠标事件.阻止动作发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆