Answered Simulate Mouse Click

  • Monday, April 09, 2012 1:26 AM
     
      Has Code

    I need to make my mouse left click. Basicly I'm making a macro type program and I need it to auto click for me. Here is the code so far but i dont think its right.

    Public Sub OpenMenu()
            'Steps 1-2 Here
            'SendKeys.Send("%T") 'Alt + t
            ' System.Windows.Forms.MouseButtons.Left()
            'SendKeys.Send("{MouseButtons.Left}")
            SendKeys.Send(MouseButtons.Left = Windows.Forms.MouseButtons.Left)
        End Sub
    Basicly what is happenign here, i will add in a mouse position this will allready be set to click on. I need this line here to left click using the mouse.

All Replies

  • Monday, April 09, 2012 1:38 AM
     
     Answered Has Code

    You're doing it wrong. Avoid SendKeys.Send() If anything, use SendKeys.SendWait(). It's much more reliable.

    To answer your question, use this to send mouse clicks:

    EDIT: Add Imports System.Runtime.InteropServices to the top. kthxbai.

    (Declarations)

     Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
        Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
        'Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public 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)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
        Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
        Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
    
        Public Const MOD_ALT As Integer = &H1 'Alt key
        Public Const WM_HOTKEY As Integer = &H312
    
      <DllImport("User32.dll")> _
        Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                            ByVal id As Integer, ByVal fsModifiers As Integer, _
                            ByVal vk As Integer) As Integer
        End Function
    
        <DllImport("User32.dll")> _
        Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                            ByVal id As Integer) As Integer
        End Function

    And in the event....

    Cursor.Position = New Point(1100, 60)
                    Threading.Thread.Sleep(1000)
                    mouse_event(&H2, 0, 0, 0, 1) ' left button down
                    Threading.Thread.Sleep(573)
                    mouse_event(&H4, 0, 0, 0, 1)

    Keep the Threading.Sleep() stuff wherever you decide to use this mouse_event. I'm not fully sure WHY it's needed but I know well enough from experience that the code will not work if it is not used. Change the cursor points to wherever you need them.

    On the side, if you want, I can send you an old program of mine that used code like this. It was a specific website macro recorder. However, if you wanted to use the code as a springboard for a real macro recorder, be my guest.

    http://www.pinvoke.net/default.aspx/user32/mouse_event.html# - Confusing yet informational documentation.

    - Jordan


    Jordan St. Godard | Microsoft® Community Contributor 2011

    double twoCents = .02;
    Console.WriteLine("$" + twoCents.ToString());


  • Monday, April 09, 2012 1:59 AM
     
      Has Code

    Very nice but Im a complete idiot lol So how would I make a call to run this event using a timer?

    Here is my code:

    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Drawing
    Imports System.Windows.Forms
    Public Class FormMenu
        Dim CropType As Integer
        Dim UseOptional As Boolean
        Dim MakeNum As Integer
        Dim StartDelay As Integer = 4
        Private Sub FormMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Location = New System.Drawing.Point(750, 20)
        End Sub
        Public Sub OpenMenu()
            'Steps 1-2 Here
            'SendKeys.Send("%T") 'Alt + t
            ' System.Windows.Forms.MouseButtons.Left()
            'SendKeys.Send("{MouseButtons.Left}")
            SendKeys.Send(MouseButtons.Left = Windows.Forms.MouseButtons.Left)
        End Sub
        Public Sub SelectCrop()
            If CropType = 1 Then
                'Position Here
                'Click
            ElseIf CropType = 2 Then
                'Position Here
                'Click
            ElseIf CropType = 3 Then
                'Position Here
                'Click
            ElseIf CropType = 4 Then
                'Position Here
                'Click
            ElseIf CropType = 5 Then
                'Position Here
                'Click
            ElseIf CropType = 6 Then
                'Position Here
                'Click
            End If
        End Sub
    #Region "Crop Types"
        Private Sub rbYellowOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbYellowOnion.CheckedChanged
            CropType = 1
        End Sub
        Private Sub rbCabbage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbCabbage.CheckedChanged
            CropType = 2
        End Sub
        Private Sub rbGreenOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGreenOnion.CheckedChanged
            CropType = 3
        End Sub
        Private Sub rbBunchofRas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBunchofRas.CheckedChanged
            CropType = 4
        End Sub
        Private Sub rbBlackberry_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBlackberry.CheckedChanged
            CropType = 5
        End Sub
        Private Sub rbUnknown_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbUnknown.CheckedChanged
            CropType = 6
        End Sub
    #End Region
        Private Sub cbOptional_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbOptional.CheckedChanged
            'Adjust Later 
            If UseOptional = False Then
                'Dont Click on Optional
            ElseIf UseOptional = True Then
                'Click on Optional
            End If
        End Sub
        Private Sub AmounttoMake()
            'Make MakeNum Amount
            'Position
            'Click
        End Sub
        Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
            If MakeNum <= 1 Then
                MakeNum = 1
            Else
                MakeNum -= 1
            End If
        End Sub
        Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            If MakeNum >= 99 Then
                MakeNum = 99
            Else
                MakeNum += 1
            End If
        End Sub
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Me.TopMost = False
            Me.Hide()
            'Start
            Delay1.Start()
        End Sub
        Private Sub Delay1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delay1.Tick
            'Delay for Alt Tabing
            If StartDelay = 0 Then
                OpenMenu()
                StartDelay = 4
                Delay1.Stop()
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            Else
                StartDelay -= 1
            End If
        End Sub
    End Class

    Basicly this section of code needs to execute the mouse click as needed or at least make a call to the sub

     Private Sub Delay1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delay1.Tick
            'Delay for Alt Tabing
            If StartDelay = 0 Then
                OpenMenu()
                StartDelay = 4
                Delay1.Stop()
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            Else
                StartDelay -= 1
            End If
        End Sub

  • Monday, April 09, 2012 3:18 AM
     
      Has Code
    Private Function MouseClick(ByVal x As Integer, ByVal y As Integer) ' As Integer?? 
    	Cursor.Position = New Point(x, y)
    	Threading.Thread.Sleep(1000)
    	mouse_event(&H2, 0, 0, 0, 1)
    	Threading.Thread.Sleep(573)
    	mouse_event(&H4, 0, 0, 0, 1)
    End Function
    
    Private Sub Delay1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delay1.Tick
            'Delay for Alt Tabing
            If StartDelay = 0 Then
                OpenMenu()
                StartDelay = 4
                Delay1.Stop()
                MouseClick(100, 100) ' Call mouseClick function here.
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            Else
                StartDelay -= 1
            End If
        End Sub
    Untested. But this should work (if this answers your question mark the previous reply as the answer rather than this reply since my first reply is relevant to the ORIGINAL question of performing mouse clicks.)

    Jordan St. Godard | Microsoft® Community Contributor 2011

    double twoCents = .02;
    Console.WriteLine("$" + twoCents.ToString());

  • Monday, April 09, 2012 3:16 PM
     
      Has Code

    Here is my code now, it works but the mouse only moves to the first point. Not sure where I went wrong here.

    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Drawing
    Imports System.Windows.Forms
    Public Class FormMenu
        Dim CropType As Integer
        Dim UseOptional As Boolean
        Dim MakeNum As Integer
        Dim StartDelay As Integer = 10
    #Region "Const"
        Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
        Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
        'Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public 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)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
        Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
        Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
        Public Const MOD_ALT As Integer = &H1 'Alt key
        Public Const WM_HOTKEY As Integer = &H312
        <DllImport("User32.dll")> _
        Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                            ByVal id As Integer, ByVal fsModifiers As Integer, _
                            ByVal vk As Integer) As Integer
        End Function
        <DllImport("User32.dll")> _
        Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        End Function
    #End Region
        Private Sub FormMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Location = New System.Drawing.Point(750, 20)
        End Sub
        Private Function OpenCraftMenuClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Threading.Thread.Sleep(1000)
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
    #Region "Apprentice"
        Private Function ApprenticeClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Threading.Thread.Sleep(1000)
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
        Private Function YellowOnionClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Threading.Thread.Sleep(1000)
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
    #End Region
    #Region "Select Crop"
        Public Sub SelectCrop()
            If CropType = 1 Then
                ApprenticeClick(245, 115)
                Threading.Thread.Sleep(1000)
                YellowOnionClick(265, 180)
            ElseIf CropType = 2 Then
                'Position Here
                'Click
            ElseIf CropType = 3 Then
                'Position Here
                'Click
            ElseIf CropType = 4 Then
                'Position Here
                'Click
            ElseIf CropType = 5 Then
                'Position Here
                'Click
            ElseIf CropType = 6 Then
                'Position Here
                'Click
            End If
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
        End Sub
    #End Region
    #Region "Crop Types"
        Private Sub rbYellowOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbYellowOnion.CheckedChanged
            CropType = 1
        End Sub
        Private Sub rbCabbage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbCabbage.CheckedChanged
            CropType = 2
        End Sub
        Private Sub rbGreenOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGreenOnion.CheckedChanged
            CropType = 3
        End Sub
        Private Sub rbBunchofRas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBunchofRas.CheckedChanged
            CropType = 4
        End Sub
        Private Sub rbBlackberry_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBlackberry.CheckedChanged
            CropType = 5
        End Sub
        Private Sub rbUnknown_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbUnknown.CheckedChanged
            CropType = 6
        End Sub
    #End Region
        Private Sub cbOptional_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbOptional.CheckedChanged
            'Adjust Later 
            If UseOptional = False Then
                'Dont Click on Optional
            ElseIf UseOptional = True Then
                'Click on Optional
            End If
        End Sub
        Private Sub AmounttoMake()
            'Make MakeNum Amount
            'Position
            'Click
        End Sub
        Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
            If MakeNum <= 1 Then
                MakeNum = 1
            Else
                MakeNum -= 1
            End If
        End Sub
        Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            If MakeNum >= 99 Then
                MakeNum = 99
            Else
                MakeNum += 1
            End If
        End Sub
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            '10 Second Start Delay
            Threading.Thread.Sleep(10000)
            OpenCraftMenuClick(80, 730)
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            '2 Second Delay
            Threading.Thread.Sleep(2000)
            SelectCrop()
        End Sub
    End Class

  • Monday, April 09, 2012 3:19 PM
     
     
    Nevermind Im a idiot lol I got it
  • Monday, April 09, 2012 7:52 PM
     
      Has Code

    Ok Final problem. Right click is not working. Here is new code

    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Drawing
    Imports System.Windows.Forms
    Public Class FormMenu
        'Add System.Speech Reference
        Dim TheSpeaker As New Speech.Synthesis.SpeechSynthesizer()
        Dim TextToSpeech As Boolean = True
        Dim CropType As Integer
        Dim UseOptional As Integer
        Dim MakeNum As Integer
    #Region "Const"
        Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
        Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
        'Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public 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)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
        Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
        Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
        Public Const MOD_ALT As Integer = &H1 'Alt key
        Public Const WM_HOTKEY As Integer = &H312
        <DllImport("User32.dll")> _
        Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                            ByVal id As Integer, ByVal fsModifiers As Integer, _
                            ByVal vk As Integer) As Integer
        End Function
        <DllImport("User32.dll")> _
        Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        End Function
    #End Region
    #Region "Load Form"
        Private Sub FormMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Location = New System.Drawing.Point(750, 20)
            MakeNum = 1
            lblMakeNum.Text = MakeNum.ToString
            TextToSpeech = True
        End Sub
    #End Region
    #Region "Open Craft Menu"
        Private Function OpenCraftMenuClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Opening Craft Menu")
            End If
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
    #End Region
    #Region "Text To Speech"
        Sub Say(ByRef t As String)
            TheSpeaker.SelectVoiceByHints(Speech.Synthesis.VoiceGender.Female, Speech.Synthesis.VoiceAge.Adult)
            TheSpeaker.Speak(t)
        End Sub
        Private Sub rbSoundOn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSoundOn.CheckedChanged
            TextToSpeech = True
        End Sub
        Private Sub rbSoundOff_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSoundOff.CheckedChanged
            TextToSpeech = False
        End Sub
    #End Region
    #Region "Apprentice"
        Private Function ApprenticeClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(245, 115)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Apprentice")
            End If
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
        Private Function VegetablesClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(255, 162)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Vegetables")
            End If
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
        Private Function YellowOnionClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(265, 180)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Yellow Onion")
            End If
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
    #End Region
    #Region "Select Crop"
        Public Sub SelectCrop()
            If CropType = 1 Then
                ApprenticeClick(245, 110)
                Threading.Thread.Sleep(1000)
                VegetablesClick(255, 162)
                Threading.Thread.Sleep(1000)
                YellowOnionClick(265, 180)
                Threading.Thread.Sleep(1000)
                Optionalchecker()
            ElseIf CropType = 2 Then
                'Position Here
                'Click
            ElseIf CropType = 3 Then
                'Position Here
                'Click
            ElseIf CropType = 4 Then
                'Position Here
                'Click
            ElseIf CropType = 5 Then
                'Position Here
                'Click
            ElseIf CropType = 6 Then
                'Position Here
                'Click
            End If
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
        End Sub
    #End Region
    #Region "Crop Types"
        Private Sub rbYellowOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbYellowOnion.CheckedChanged
            CropType = 1
        End Sub
        Private Sub rbCabbage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbCabbage.CheckedChanged
            CropType = 2
        End Sub
        Private Sub rbGreenOnion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGreenOnion.CheckedChanged
            CropType = 3
        End Sub
        Private Sub rbBunchofRas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBunchofRas.CheckedChanged
            CropType = 4
        End Sub
        Private Sub rbBlackberry_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBlackberry.CheckedChanged
            CropType = 5
        End Sub
        Private Sub rbUnknown_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbUnknown.CheckedChanged
            CropType = 6
        End Sub
    #End Region
    #Region "Use Optional"
        Private Sub rbYes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbYes.CheckedChanged
            UseOptional = 1
        End Sub
        Private Sub rbNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNo.CheckedChanged
            UseOptional = 0
        End Sub
        Public Sub Optionalchecker()
            If UseOptional = 0 Then
                'skip
            ElseIf UseOptional = 1 Then
                UseOptionalClick(935, 410)
            End If
            AmounttoMake(850, 540)
        End Sub
        Private Function UseOptionalClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(935, 410)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("use optional")
            End If
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
        End Function
    #End Region
    #Region "Make"
        Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
            If MakeNum <= 1 Then
                MakeNum = 1
            Else
                MakeNum -= 1
            End If
            lblMakeNum.Text = MakeNum.ToString
        End Sub
        Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            If MakeNum >= 6 Then
                MakeNum = 6
            Else
                MakeNum += 1
            End If
            lblMakeNum.Text = MakeNum.ToString
        End Sub
        Private Sub AmounttoMake(ByVal x As Integer, ByVal y As Integer)
            If MakeNum = 1 Then
                'Skip
                Cursor.Position = New Point(900, 540)
            ElseIf MakeNum = 2 Then
                Cursor.Position = New Point(850, 535)
                Threading.Thread.Sleep(1000)
                '+1
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
            ElseIf MakeNum = 3 Then
                Cursor.Position = New Point(850, 535)
                Threading.Thread.Sleep(1000)
                '+1
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+2
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
            ElseIf MakeNum = 4 Then
                Cursor.Position = New Point(850, 535)
                Threading.Thread.Sleep(1000)
                '+1
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+2
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+3
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
            ElseIf MakeNum = 5 Then
                Cursor.Position = New Point(850, 535)
                Threading.Thread.Sleep(1000)
                '+1
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+2
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+3
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+4
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
            ElseIf MakeNum = 6 Then
                Cursor.Position = New Point(850, 535)
                Threading.Thread.Sleep(1000)
                '+1
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+2
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+3
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+4
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
                '+5
                Threading.Thread.Sleep(573)
                mouse_event(&H2, 0, 0, 0, 1)
                Threading.Thread.Sleep(573)
                mouse_event(&H4, 0, 0, 0, 1)
            End If
            Threading.Thread.Sleep(1000)
            MakeClick(900, 535)
        End Sub
        Private Function MakeClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(900, 535)
            Threading.Thread.Sleep(1000)
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
            CloseMenuClick(1010, 35)
        End Function
    #End Region
    #Region "Close Menu"
        Private Function CloseMenuClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(1010, 30)
            Threading.Thread.Sleep(1000)
            mouse_event(&H2, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H4, 0, 0, 0, 1)
            'Begin Planting 
            Threading.Thread.Sleep(1000)
            PlantingDelay()
        End Function
    #End Region
    #Region "Planting"
        Public Sub PlantCrop()
            If TextToSpeech = True Then
                Say("Planting Crops")
            End If
            Threading.Thread.Sleep(MakeNum * 20000)
        End Sub
        Public Sub PlantingDelay()
            '20 Second Per Harvest Delay
            If MakeNum = 1 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            ElseIf MakeNum = 2 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '2
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            ElseIf MakeNum = 3 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '2
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '3
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            ElseIf MakeNum = 4 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '2
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '3
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '4
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            ElseIf MakeNum = 5 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '2
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '3
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '4
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '5
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            ElseIf MakeNum = 6 Then
                'Planting Delay
                PlantCrop()
                '1
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '2
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '3
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '4
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '5
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                '6
                HarvestDelay()
                'Harvest Crops
                HarvestCropsClick(610, 480)
                'Allow Harvest To Finish
                HarvestDelay()
                EndHarvest()
            End If
        End Sub
    #End Region
    #Region "Harvest"
        Public Sub HarvestDelay()
            System.Threading.Thread.Sleep(12000)
            If TextToSpeech = True Then
                Say("Harvest Delay Over")
            End If
        End Sub
        Private Function HarvestCropsClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(610, 485)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Harvesting Crops")
            End If
            'Right Click to Harvest
            mouse_event(&H8, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H10, 0, 0, 0, 1)
        End Function
        Public Sub EndHarvest()
            MakeNum = 0
            lblMakeNum.Text = MakeNum.ToString
            If TextToSpeech = True Then
                Say("Harvest Complete")
            End If
            btnMinus.Enabled = True
            btnPlus.Enabled = True
        End Sub
    #End Region
    #Region "Start Button"
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            btnMinus.Enabled = False
            btnPlus.Enabled = False
            '10 Second Start Delay
            Threading.Thread.Sleep(10000)
            OpenCraftMenuClick(80, 730)
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            '2 Second Delay
            Threading.Thread.Sleep(2000)
            SelectCrop()
        End Sub
    #End Region
    End Class

    This section right here is the issue. I assume something with the 0.0.0.1?

    Private Function HarvestCropsClick(ByVal x As Integer, ByVal y As Integer)
            Cursor.Position = New Point(x, y)
            Cursor.Position = New Point(610, 485)
            Threading.Thread.Sleep(1000)
            If TextToSpeech = True Then
                Say("Harvesting Crops")
            End If
            'Right Click to Harvest
            mouse_event(&H8, 0, 0, 0, 1)
            Threading.Thread.Sleep(573)
            mouse_event(&H10, 0, 0, 0, 1)
        End Function

  • Monday, April 09, 2012 8:23 PM
     
     

    I wanted to post before but decided not to meddle. However, the more the code grows... :)

    • You have much better chances for an answer if you make the code compilable. Compliable with Option Strict On
    • A function is a method returning a value. Otherwise it's a sub.
    • If you don't use SetCursorPos and GetCursorPos delete the declarations as they are wrong.
    • mouse_event has been superseeded by SendInput long ago (according to it's documentation).
    • The declaration of mouse_event is wrong. The currrent version only runs correctly as a 32 bit process. Last parameter must be of type IntPtr.
    • Does the "1" have a meaning passed as the last argument to mouse_event?
    • Too many #regions can decreases readability, but of course that's your choice and must match your own preferences.
    • Select Case simplifies some If..elseif..elsif..End If blocks.
    • You're repeating many blocks many times. Write (parameterized) methods instead and call them. This way it's hard to get an overview.
    • Use the constants declared at the beginning. Nobody knows by heart what &H2 means in "mouse_event(&H2, 0, 0, 0, 1)"
    • I hope this was not too hard. ;)

    Armin

  • Thursday, April 12, 2012 1:07 AM
     
     
    I found the problem. If you play Lotro and farm. You must right click quickly to trigger the harvest. The right clickmouse down washolding the key down too long. adjusted the timeout and works great now. Thanks for all the support/posts everyone. If you play Lotro, this code should work for you to macro farming using a 1024/768 res.