Monitor Mouse Movement and Clicks

Use this board to post your code snippets - tips and tricks
3 posts Page 1 of 1
Contributors
MANSQN
VIP - Donator
VIP - Donator
Posts: 159
Joined: Sat Sep 17, 2011 11:33 pm

Monitor Mouse Movement and Clicks
MANSQN
This project will monitor the mouse movement on the screen, inside and outside the form, and show the movement on a small form:

Requirments: A Form, Button1, Button2, Timer1
Code: Select all
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_MIDDLEDOWN = &H20

    Private Const MOUSEEVENTF_MIDDLEUP = &H40

    Private Const MOUSEEVENTF_RIGHTDOWN = &H8

    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

        UnhookWindowsHookEx(_mouseHook)

        _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(lParam.pt.X * formw / screenw, lParam.pt.Y * formh / screenh)

        Try

            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

        RemoveHook()

    End Sub

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

        InstallHook()

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

    End Sub
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Monitor Mouse Movement and Clicks
mandai
It doesn't look like a timer is needed here.
Why is mouse_event declared if it is not used?
MANSQN
VIP - Donator
VIP - Donator
Posts: 159
Joined: Sat Sep 17, 2011 11:33 pm

Re: Monitor Mouse Movement and Clicks
MANSQN
mandai wrote:
It doesn't look like a timer is needed here.
Why is mouse_event declared if it is not used?
this is not my code. but i think i'll fix it then repost it.

-MANSQN-
3 posts Page 1 of 1
Return to “Quick Snips”