dectect mouse move

Chris11

Active member
Joined
Jul 12, 2009
Messages
29
Programming Experience
1-3
I need to be able to find out if the mouse has been moved, if the mouse hasn't been moved in a minute then use Cursor.Hide() to hide the mouse, if the mouse is moving then I want the mouse to be shown.

To get the Position of the mouse use this:

VB.NET:
Cursor.Position.X

VB.NET:
Cursor.Position.Y

I don't know how to do this :confused:
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
This is a very basic code. For advance idle detector you may want to google

VB.NET:
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If mousehidden = True Then
            Cursor.Show()
            sleep = 10
            mousehidden = False
            Timer1.Interval = 1000
            Timer1.Start()
        End If
    End Sub

    Private sleep As Integer = 10
    Private mousehidden As Boolean = False

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If sleep > 0 Then
            sleep -= 1
        Else
            mousehidden = True
            Cursor.Hide()
            Timer1.Stop()
        End If
    End Sub
 

Chris11

Active member
Joined
Jul 12, 2009
Messages
29
Programming Experience
1-3
I've copied the code in and it dosn't want to work. :(

It might have something to do with the
VB.NET:
mousehidden = True
Because the only place you told it that mousehidden = True is in Timer1. But mousehidden needs to be true for Timer1 to be active.

Is that right or wrong?:confused:
 

Chris11

Active member
Joined
Jul 12, 2009
Messages
29
Programming Experience
1-3
Ok, it does work, but not on my program.... Would anyone have any idea why?

This is the full code of the form

VB.NET:
Public Class LockWindow

    Private Sub LockWindow_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.Reload()
        If My.Settings.AllowClose = "True" Then
            e.Cancel = False
            My.Settings.AllowClose = "False"
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub LockWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LockSettings.Icon.Visible = False
        My.Settings.Reload()
        Me.WindowState = FormWindowState.Maximized
        Me.TopMost = True
        Dim FormHeight As String = Me.Height()
        Dim FormWidth As String = Me.Width()
        Dim Login_Box_Height As String = Login_Box.Height()
        Dim Login_Box_Width As String = Login_Box.Width()
        Login_Box.Location = New Point(x:=(Val(FormWidth) / Val(2)) - (Val(Login_Box_Width) / Val(2)), y:=(Val(FormHeight) / Val(2)) - (Val(Login_Box_Height) / Val(2)))

        If My.Settings.HidePassword = "True" Then
            Password.UseSystemPasswordChar = True
        Else
            Password.UseSystemPasswordChar = False
        End If

        If My.Settings.UseImage = "True" Then
            If My.Settings.UseOwnImage = "True" Then
                LockPicture.ImageLocation = My.Settings.OwnImage
            Else
                If My.Settings.PreImage = "Lighting" Then
                    LockPicture.Image = My.Resources.Lighting
                End If

                If My.Settings.PreImage = "Background" Then
                    LockPicture.Image = My.Resources.Background
                End If

                If My.Settings.PreImage = "Stars" Then
                    LockPicture.Image = My.Resources.Background_Stars
                End If

                If My.Settings.PreImage = "Blue Background" Then
                    LockPicture.Image = My.Resources.Blue_Background
                End If

                If My.Settings.PreImage = "Business" Then
                    LockPicture.Image = My.Resources.Business
                End If

                If My.Settings.PreImage = "Colourful" Then
                    LockPicture.Image = My.Resources.Colourful
                End If

                If My.Settings.PreImage = "Fire" Then
                    LockPicture.Image = My.Resources.Fire
                End If

                If My.Settings.PreImage = "Alienware" Then
                    LockPicture.Image = My.Resources.Alienware
                End If

                If My.Settings.PreImage = "Brazil - Rio de Janeiro" Then
                    LockPicture.Image = My.Resources.Brazil___Rio_de_Janeiro
                End If

                If My.Settings.PreImage = "Eiffel Tower Night" Then
                    LockPicture.Image = My.Resources.Eiffel_Tower__Night
                End If

                If My.Settings.PreImage = "Fighter Jet" Then
                    LockPicture.Image = My.Resources.Fighter_jet
                End If

                If My.Settings.PreImage = "Fiji" Then
                    LockPicture.Image = My.Resources.Fiji
                End If

                If My.Settings.PreImage = "Great Wall of China" Then
                    LockPicture.Image = My.Resources.Great_Wall_of_China
                End If

                If My.Settings.PreImage = "Microsoft Windows XP Gold" Then
                    LockPicture.Image = My.Resources.Microsoft_Windows_XP_Gold
                End If

                If My.Settings.PreImage = "San Francisco's Golden Gate Bridge" Then
                    LockPicture.Image = My.Resources.San_Francisco_s_Golden_Gate_bridge_at_night
                End If

                If My.Settings.PreImage = "Vista Eclipse" Then
                    LockPicture.Image = My.Resources.Vista_Eclipse
                End If

                If My.Settings.PreImage = "Vista Themes" Then
                    LockPicture.Image = My.Resources.Vista_Themes
                End If

                If My.Settings.PreImage = "Windows Vista Aqua" Then
                    LockPicture.Image = My.Resources.Windows_Vista_Aqua
                End If
            End If
        Else
        End If
        Me.KeyPreview = True
        If My.Settings.ShowHideButton = "True" Then
            LoginHide.Visible = True
        End If
        If My.Settings.ShowHideButton = "False" Then
            LoginHide.Visible = False
        End If
    End Sub

    Private Sub Unlock_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Unlock_Button.Click
        My.Settings.Reload()
        Dim Time = TimeOfDay
        Dim Today = DateString
        My.Settings.Reload()
        If Password.Text = My.Settings.AdminPassword Then
            My.Settings.AllowClose = "True"
            My.Settings.Save()
            Me.Close()
            Settings.Enabled = False
            Me.TopMost = False
            LockSettings.Icon.Visible = True
        Else
            If My.Settings.PlaySiren = "True" Then
                My.Computer.Audio.Play(My.Resources.Alarm_Signal, AudioPlayMode.Background)
            End If
            If My.Settings.ShowUnsuccessfulAttempts = "True" Then
                Unsuccessful_Attempts.Show()
                Unsuccessful_Attempts.AttemptsList.Items.Add(Today + ", " + Time + ", Unsuccessful Attempt")

            End If
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginHide.Click
        Login_Box.Visible = False
        ShowLogin.Enabled = True
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F12 Then
        Else
            Login_Box.Visible = True
        End If
        If e.KeyCode = Keys.Enter Then

            My.Settings.Reload()
            Dim Time = TimeOfDay
            Dim Today = DateString
            My.Settings.Reload()
            If Password.Text = My.Settings.AdminPassword Then
                My.Settings.AllowClose = "True"
                My.Settings.Save()
                TimedLock.Close()
                Me.Close()
                Settings.Enabled = False
                Me.TopMost = False
                LockSettings.Icon.Visible = True
            Else
                My.Computer.Audio.Play(My.Resources.Alarm_Signal, AudioPlayMode.Background)

                If My.Settings.ShowUnsuccessfulAttempts = "True" Then
                    Unsuccessful_Attempts.Show()
                    Unsuccessful_Attempts.AttemptsList.Items.Add(Today + ", " + Time + ", Unsuccessful Attempt")
                End If

            End If

        End If
    End Sub

    Private Sub Settings_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Settings.Tick
        Me.TopMost = True
    End Sub

    Private Sub Mouse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mouse.Tick
        Dim MousePositionX As Integer = Cursor.Position.X
        Dim MousePositionY As Integer = Cursor.Position.Y

        If MousePositionX > 1181 Then
            Windows.Forms.Cursor.Position = New Point(1181, MousePositionY)
        End If

        If MousePositionX < 739 Then
            Windows.Forms.Cursor.Position = New Point(739, MousePositionY)
        End If

        If MousePositionY < 526 Then
            Windows.Forms.Cursor.Position = New Point(MousePositionX, 526)
        End If

        If MousePositionY > 672 Then
            Windows.Forms.Cursor.Position = New Point(MousePositionX, 672)
        End If
    End Sub

    Private sleep As Integer = 1
    Private mousehidden As Boolean = False

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If sleep > 0 Then
            sleep -= 1
        Else
            mousehidden = True
            Cursor.Hide()
            Timer1.Stop()
        End If
    End Sub

    Private Sub LockWindow_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If mousehidden = True Then
            Cursor.Show()
            sleep = 10
            mousehidden = False
            Timer1.Interval = 100
            Timer1.Start()
        End If
    End Sub
End Class

The MouseMove event is not happening for some reason??? :(
 
Last edited:

Chris11

Active member
Joined
Jul 12, 2009
Messages
29
Programming Experience
1-3
Bingo! Thanks so much, the mouse was on the picture box, so I added the code into the picture box mouse move event as well, and I did the same for the group box. Thanks once again!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,465
Location
Norway
Programming Experience
10+
Bingo! Thanks so much, the mouse was on the picture box, so I added the code into the picture box mouse move event as well, and I did the same for the group box. Thanks once again!
To prevent having to handle this for many controls you can rearrange things a bit and cache the information needed to know if cursor has moved and how long the idle time is, to do this you need a Point variable and a Date variable. The Timer needs to tick all the time. In addition, to balance the cursor Show/Hide calls you need a variable to keep track of its visibility state. Here is the complete code sample:
VB.NET:
Private loc As Point, idle As Date, hidden As Boolean

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If loc <> Cursor.Position Then
        If hidden Then
            Cursor.Show()
            hidden = False
        End If
        loc = Cursor.Position
        idle = Date.Now
    ElseIf Not hidden AndAlso (Date.Now - idle).TotalSeconds > 3 Then            
        Cursor.Hide()
        hidden = True
    End If
End Sub
This Timer can tick each 1/2-1 seconds depending on how responsive you want it, the idle time is set to 3 seconds. The code should be easy to understand when you read it and give it some thought, if not ask.
 
Top Bottom