none
How do I use a timer from a button press? RRS feed

  • Question

  • I would like to use a timer, so that the mouse moves every minute once the button is pressed.

    Public Class Form1
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwflags As Long)

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim InfiniteLoop As Boolean = True

            For i = 1 To 45687894
                If i = 45687893 And InfiniteLoop = True Then i = 1
                Cursor.Position = New Point(200, 200)
                Cursor.Position = New Point(400, 400)
            Next

        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Me.KeyDown, AddressOf CheckKeyDown
            For Each C As Control In Me.Controls
                AddHandler C.KeyDown, AddressOf CheckKeyDown
            Next
        End Sub
        Sub CheckKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.KeyCode = Keys.Escape Then
                Me.Close()
            End If
        End Sub
    End Class

    Wednesday, November 14, 2018 4:39 PM

Answers

  • Add this and put the code to move the mouse where shown

        Private MoveMouseTask As task
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            MoveMouseTask = Task.Run(Sub()
                                         _MoveMouse()
                                     End Sub)
        End Sub
    
        Private Sub _MoveMouse()
            Dim toggle As Boolean = False
            Do
                Threading.Thread.Sleep(60 * 1000) 'one minute
                'code to move mouse here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                toggle = Not toggle
                If toggle Then
                    Cursor.Position = New Point(200, 200)
                Else
                    Cursor.Position = New Point(400, 400)
                End If
            Loop
        End Sub
    

    Depending on the Form you might not need to check for the Escape key.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Wednesday, November 14, 2018 7:22 PM
    • Marked as answer by VBShaper Thursday, November 15, 2018 11:26 AM
    Wednesday, November 14, 2018 5:24 PM

All replies

  • Wednesday, November 14, 2018 5:00 PM
  • Hi

    Since you have managed to accrue 35 points, you should know by now to use the code block tool to post code.


    Regards Les, Livingston, Scotland

    Wednesday, November 14, 2018 5:01 PM
  • Add this and put the code to move the mouse where shown

        Private MoveMouseTask As task
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            MoveMouseTask = Task.Run(Sub()
                                         _MoveMouse()
                                     End Sub)
        End Sub
    
        Private Sub _MoveMouse()
            Dim toggle As Boolean = False
            Do
                Threading.Thread.Sleep(60 * 1000) 'one minute
                'code to move mouse here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                toggle = Not toggle
                If toggle Then
                    Cursor.Position = New Point(200, 200)
                Else
                    Cursor.Position = New Point(400, 400)
                End If
            Loop
        End Sub
    

    Depending on the Form you might not need to check for the Escape key.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Wednesday, November 14, 2018 7:22 PM
    • Marked as answer by VBShaper Thursday, November 15, 2018 11:26 AM
    Wednesday, November 14, 2018 5:24 PM
  • Hello,

    Hope below code helps you

    Imports System.ComponentModel
    
    Public Class Form20
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwflags As Long)
        Private Sub Form20_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Interval = 60000
            AddHandler Me.KeyDown, AddressOf CheckKeyDown
            For Each C As Control In Me.Controls
                AddHandler C.KeyDown, AddressOf CheckKeyDown
            Next
        End Sub
        Sub CheckKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.KeyCode = Keys.Escape Then
                Me.Close()
            End If
        End Sub
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
        End Sub
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Try
                Cursor.Position = New Point(200, 200)
                Cursor.Position = New Point(400, 400)
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class

    Wednesday, November 14, 2018 6:35 PM
  • Add this and put the code to move the mouse where shown

        Private MoveMouseTask As task
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            MoveMouseTask = Task.Run(Sub()
                                         _MoveMouse()
                                     End Sub)
        End Sub
    
        Private Sub _MoveMouse()
            Dim toggle As Boolean = False
            Do
                Threading.Thread.Sleep(60 * 1000) 'one minute
                'code to move mouse here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                toggle = Not toggle
                If toggle Then
                    Cursor.Position = New Point(200, 200)
                Else
                    Cursor.Position = New Point(400, 400)
                End If
            Loop
        End Sub
    

    Depending on the Form you might not need to check for the Escape key.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    I made a little bit of a change but it does what I need. Thank you all for your help.

    Private MoveMouseTask As Task
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            MoveMouseTask = Task.Run(Sub()
                                         _MoveMouse()
                                     End Sub)
        End Sub
    
        Private Sub _MoveMouse()
            Dim toggle As Boolean = False
            Dim InfiniteLoop As Boolean = True
            Do
               
    
    
                For i = 1 To 45687894
                    If i = 45687893 And InfiniteLoop = True Then i = 1
                    Cursor.Position = New Point(200, 200)
                    Threading.Thread.Sleep(30 * 1000)
                    Cursor.Position = New Point(400, 400)
                    Threading.Thread.Sleep(30 * 1000)
                    Cursor.Position = New Point(200, 200)
                    'one minute
                Next
    
    
    
    
                toggle = Not toggle
                If toggle Then
                    Cursor.Position = New Point(200, 200)
                Else
                    Cursor.Position = New Point(400, 400)
                End If
            Loop
        End Sub
    End Class

    Thursday, November 15, 2018 11:29 AM
  • Why do you have that busy loop in the code?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Thursday, November 15, 2018 1:17 PM