Advertisement
Guest User

HOOK

a guest
Apr 1st, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.65 KB | None | 0 0
  1. Public Class MouseDetector
  2.     Public Event MouseRightButtonUp(ByVal sender As Object, ByVal e As MouseEventArgs)
  3.     Public Event MouseRightButtonDown(ByVal sender As Object, ByVal e As MouseEventArgs)
  4.     Public Event MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseEventArgs)
  5.     Public Event MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseEventArgs)
  6.     Private Delegate Function MouseHookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  7.     Private MouseHookCallbackDelegate As MouseHookCallback
  8.     Private MouseHookID As Integer
  9.     Public Sub New()
  10.         If MouseHookID = 0 Then
  11.             MouseHookCallbackDelegate = AddressOf MouseHookProc
  12.             MouseHookID = SetWindowsHookEx(CInt(14), MouseHookCallbackDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
  13.             If MouseHookID = 0 Then
  14.                 'error
  15.             End If
  16.         End If
  17.     End Sub
  18.     Public Sub Dispose()
  19.         If Not MouseHookID = -1 Then
  20.             UnhookWindowsHookEx(MouseHookID)
  21.             MouseHookCallbackDelegate = Nothing
  22.         End If
  23.         MouseHookID = -1
  24.     End Sub
  25.     Private Enum MouseMessages
  26.         WM_LeftButtonDown = 513
  27.         WM_LeftButtonUp = 514
  28.         WM_LeftDblClick = 515
  29.         WM_RightButtonDown = 516
  30.         WM_RightButtonUp = 517
  31.         WM_RightDblClick = 518
  32.     End Enum
  33.     <StructLayout(LayoutKind.Sequential)>
  34.     Private Structure Point
  35.         Public x As Integer
  36.         Public y As Integer
  37.     End Structure
  38.     <StructLayout(LayoutKind.Sequential)>
  39.     Private Structure MouseHookStruct
  40.         Public pt As Point
  41.         Public hwnd As Integer
  42.         Public wHitTestCode As Integer
  43.         Public dwExtraInfo As Integer
  44.     End Structure
  45.     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
  46.     Private Shared Function CallNextHookEx(
  47.          ByVal idHook As Integer,
  48.          ByVal nCode As Integer,
  49.          ByVal wParam As IntPtr,
  50.           ByVal lParam As IntPtr) As Integer
  51.     End Function
  52.     <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)>
  53.     Private Shared Function SetWindowsHookEx _
  54.           (ByVal idHook As Integer, ByVal HookProc As MouseHookCallback,
  55.            ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
  56.     End Function
  57.     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)>
  58.     Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
  59.     End Function
  60.     Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  61.         If nCode < 0 Then
  62.             Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
  63.         End If
  64.         Dim MouseData As MouseHookStruct = Marshal.PtrToStructure(lParam, GetType(MouseHookStruct))
  65.         Select Case wParam
  66.             Case MouseMessages.WM_LeftButtonUp
  67.                 RaiseEvent MouseLeftButtonUp(Nothing, New MouseEventArgs(MouseButtons.Left, 1, MouseData.pt.x, MouseData.pt.y, 0))
  68.             Case MouseMessages.WM_LeftButtonDown
  69.                 RaiseEvent MouseLeftButtonDown(Nothing, New MouseEventArgs(MouseButtons.Left, 1, MouseData.pt.x, MouseData.pt.y, 0))
  70.             Case MouseMessages.WM_RightButtonDown
  71.                 RaiseEvent MouseRightButtonDown(Nothing, New MouseEventArgs(MouseButtons.Right, 1, MouseData.pt.x, MouseData.pt.y, 0))
  72.         End Select
  73.         Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
  74.     End Function
  75. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement