954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Tracking mouse movement in VB

Hello
i have a problem, i need the code for tracking the mouse movement in VB 2008.
my program needs to make a pop-up window when the mouse have moven an inch ( ex.)

some code suggestions is needed, thank you

- Martin

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Hello i have a problem, i need the code for tracking the mouse movement in VB 2008. my program needs to make a pop-up window when the mouse have moven an inch ( ex.)

some code suggestions is needed, thank you

- Martin

And it needs to go out side of the form

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

you can track the mouse by using this method
=======================================

Public Function GetMousePos() As Point
        Return Cursor.Position
End Function

=======================================
this will return the position of the mouse on the screen.

BoemaN
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks, but i'll also need it to make a pop-up when tho mouse has moved an inch (ex)

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

if you want to do that, You need to make some kind of a loop
you can create loops in different ways but i like to create a thread which contains a loop.

Private Shared _savedMousePosition As Point

    Public Shared Sub Main()
        'We save the current position when the app starts
        'so the popup will not popup directly
        _savedMousePosition = Cursor.Position
        Call New Thread(AddressOf MouseCheck).Start()
        Process.GetCurrentProcess.WaitForExit()
    End Sub

    Private Shared Sub MouseCheck()
        'We are doing a loop here to check if the mouse has moved a certain amount of pixels
        'if so then show the popup and reset the saved position.
        While True
            'First check if the current mouse position has a distance of 20pixels from the saved position
            'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)
            If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then
                MsgBox("Mouse moved 20 pixels!")

                'here we reset the saved position to the current mouseposition and
                'start all over again...
                _savedMousePosition = Cursor.Position
            End If
            'we dont need a lot of checks this will only couse a high CPU usage
            'so lets check the position 60 times a second
            Thread.Sleep(1000 / 60)
        End While
    End Sub

    Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single
        Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))
    End Function
BoemaN
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

do i just need to paste that code in the Form1 string?

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

you can copy the MouseCheck and distance method into the class of your form and the content of the Main method to Form1_Load

BoemaN
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

okay, thank you very much (:

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

it brings an error up om this line
Thread.Sleep(1000 / 60)
what cna i do there ?

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

the problems i get:
Thread.Sleep(1000 / 60)
- name "Thread" is not declared

Public Shared Sub Main()
-satatement cannot appear within a method body, end og method assumed

Call New Thread(AddressOf MouseCheck).Start()
- type "thread" is not defined

Here is my code:

Public Class Form1
    Private Shared _savedMousePosition As Point

    Private Shared Sub MouseCheck()

        'We are doing a loop here to check if the mouse has moved a certain amount of pixels

        'if so then show the popup and reset the saved position.

        While True

            'First check if the current mouse position has a distance of 20pixels from the saved position

            'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)

            If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then

                MsgBox("Mouse moved 20 pixels!")



                'here we reset the saved position to the current mouseposition and

                'start all over again...

                _savedMousePosition = Cursor.Position

            End If

            'we dont need a lot of checks this will only couse a high CPU usage

            'so lets check the position 60 times a second

            Thread.Sleep(1000 / 60)

        End While

    End Sub



    Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single

        Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))

    End Function

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

    Public Shared Sub Main()

        'We save the current position when the app starts

        'so the popup will not popup directly

        _savedMousePosition = Cursor.Position

        Call New Thread(AddressOf MouseCheck).Start()

        Process.GetCurrentProcess.WaitForExit()


    End Sub
End Class
Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Import System.Threading
or change the Thread to Threading.Thread

BoemaN
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Import System.Threading
or change the Thread to Threading.Thread

BoemaN
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

It works now, thank you again (:!

Kristensen292
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You