Intercepting mouse events using a global hook. Stop an action from happening

雨燕双飞 提交于 2020-01-04 05:45:08

问题


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.



来源:https://stackoverflow.com/questions/2080617/intercepting-mouse-events-using-a-global-hook-stop-an-action-from-happening

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!