Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- Public Delegate Function CallBack( _
- ByVal nCode As Integer, _
- ByVal wParam As IntPtr, _
- ByVal lParam As IntPtr) As Integer
- Public Class Form1
- 'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
- Dim WH_JOURNALRECORD As Integer = 0
- Dim WH_JOURNALPLAYBACK As Integer = 1
- Dim WH_KEYBOARD As Integer = 2
- Dim WH_GETMESSAGE As Integer = 3
- Dim WH_CALLWNDPROC As Integer = 4
- Dim WH_CBT As Integer = 5
- Dim WH_SYSMSGFILTER As Integer = 6
- Dim WH_MOUSE As Integer = 7
- Dim WH_HARDWARE As Integer = 8
- Dim WH_DEBUG As Integer = 9
- Dim WH_SHELL As Integer = 10
- Dim WH_FOREGROUNDIDLE As Integer = 11
- Dim WH_CALLWNDPROCRET As Integer = 12
- Dim WH_KEYBOARD_LL As Integer = 13
- Dim WH_MOUSE_LL As Integer = 14
- Shared hHook As Integer = 0
- 'Keep the reference so that the delegate is not garbage collected.
- Private hookproc As CallBack
- 'Import for the SetWindowsHookEx function.
- <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
- Public Overloads Shared Function SetWindowsHookEx _
- (ByVal idHook As Integer, ByVal HookProc As CallBack, _
- ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
- End Function
- 'Import for the CallNextHookEx function.
- <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
- Public Overloads Shared Function CallNextHookEx _
- (ByVal idHook As Integer, ByVal nCode As Integer, _
- ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
- End Function
- 'Import for the UnhookWindowsHookEx function.
- <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
- Public Overloads Shared Function UnhookWindowsHookEx _
- (ByVal idHook As Integer) As Boolean
- End Function
- 'Point structure declaration.
- <StructLayout(LayoutKind.Sequential)> Public Structure Point
- Public x As Integer
- Public y As Integer
- End Structure
- 'MouseHookStruct structure declaration.
- <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
- Public pt As Point
- Public hwnd As Integer
- Public wHitTestCode As Integer
- Public dwExtraInfo As Integer
- End Structure
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Me.RichTextBox1.AppendText("[" & Now & "] START LOGGING" & vbCrLf)
- If hHook.Equals(0) Then
- hookproc = AddressOf MouseHookProc
- hHook = SetWindowsHookEx(WH_MOUSE, _
- hookproc, _
- IntPtr.Zero, _
- AppDomain.GetCurrentThreadId())
- If hHook.Equals(0) Then
- Me.RichTextBox1.AppendText("SetWindowsHookEx Failed")
- Return
- End If
- Else
- Dim ret As Boolean = UnhookWindowsHookEx(hHook)
- If ret.Equals(False) Then
- Me.RichTextBox1.AppendText("UnhookWindowsHookEx Failed")
- Return
- Else
- hHook = 0
- End If
- End If
- End Sub
- Private Function FocusChanged(ByVal oldhwnd As Integer, ByVal oldpid As Integer, ByVal newhwnd As Integer, ByVal newpid As Integer) As Boolean
- Return True
- End Function
- Public Shared Function MouseHookProc( _
- ByVal nCode As Integer, _
- ByVal wParam As IntPtr, _
- ByVal lParam As IntPtr) As Integer
- Dim MyMouseHookStruct As New MouseHookStruct()
- If (nCode < 0) Then
- Return CallNextHookEx(hHook, nCode, wParam, lParam)
- End If
- MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct)
- Dim tempForm As Form
- tempForm = Form.ActiveForm()
- Dim strCaption As String
- strCaption = "x = " & MyMouseHookStruct.pt.x & " y = " & MyMouseHookStruct.pt.y
- tempForm.Text = strCaption
- Return CallNextHookEx(hHook, nCode, wParam, lParam)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment