Visual Basic 2008 Tutorials


Back | Index

Monitor Mouse Movement and Clicks

This project will monitor the mouse movement on the screen, inside and outside the form, and show the movement on a small form:

The Above Shows the mouse movement on the whole screen

Requirments: A Form, Button1, Button2, Timer1



Imports System.Runtime.InteropServices

 Public Class Form1

    Dim result As Integer

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

    Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

    Private Const MOUSEEVENTF_ABSOLUTE = &H8000

    Private Const MOUSEEVENTF_LEFTDOWN = &H2

    Private Const MOUSEEVENTF_LEFTUP = &H4

    Private Const MOUSEEVENTF_MOVE = &H1


    Private Const MOUSEEVENTF_MIDDLEUP = &H40


    Private Const MOUSEEVENTF_RIGHTUP = &H10

    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 CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32

    <MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack

    Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, 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 InstallHook() As Boolean

        If _mouseHook = IntPtr.Zero Then

            _mouseProc = New CallBack(AddressOf MouseHookProc)

            _mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)

        End If

        Return _mouseHook <> IntPtr.Zero

    End Function


    Public Sub RemoveHook()

        If _mouseHook = IntPtr.Zero Then Return


        _mouseHook = IntPtr.Zero

    End Sub


    Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32

        Dim formw As String

        Dim formh As String

        formw = Me.Width

        formh = Me.Height


        Dim screenw As String

        Dim screenh As String

        screenw = My.Computer.Screen.Bounds.Width

        screenh = My.Computer.Screen.Bounds.Height


        Dim w As String

        Dim h As String


        w = screenw / formw

        h = screenh / formh

        Button1.Location = New Point( * formw / screenw, * formh / screenh)


            For i = 1 To 255

                result = 0

                result = GetAsyncKeyState(i)


                If result = -32767 Then

                    Button2.Location = Button1.Location

                End If

            Next i

        Catch ex As Exception

        End Try

    End Function

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Me.Size = New Size(My.Computer.Screen.Bounds.Width / 5, My.Computer.Screen.Bounds.Height / 5)

    End Sub


End Class

The Above code is a collection from 3 resources:

This Site: