locked
auto right click in another application RRS feed

  • Question

  • hello

    i want to right click in another application i know the application name in process only

    i want to make many clicks by give it the X and Y where it will click every 3 seconds .

    thank you

    Monday, May 6, 2013 9:52 PM

Answers

  • Hi,

     I am not quite clear on what it is you want to do? Do you want to give it a list of X and Y positions and have it click the mouse on the next position in the list every 3 seconds or do you want to give it 1 position and click that position every 3 seconds? You will also need to think of when and how you want to tell your program to stop the mouse clicking process such as by pressing a hotkey, or after a certain number of clicks, or maybe after some other kind of event happens. Here is an example code to right click on one position of the screen every 3 seconds. I set it up to stop after 10 clicks because it can get a little tricky to close the program when the mouse keeps moving every 3 seconds. Thats why you need to figure how or when you want it to stop. I also included the Middle and Left mouse constants if you want to click a button other than the right.  :)

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Dim count As Integer = 0
        Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Const MOUSEEVENTF_LEFTUP As Int32 = &H4
        Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
        Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40
        Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
        Const MOUSEEVENTF_RIGHTUP As Int32 = &H10
    
        Private Declare Function mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As IntPtr) As Boolean
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Start the right mouse clicking timer
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Timer1.Interval = 100 Then
                Timer1.Stop()
                Timer1.Interval = 3000
                Timer1.Start()
            End If
    
            Cursor.Position = New Point(20, 37) 'Moves the mouse to this (X, Y) position
    
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, IntPtr.Zero) 'Presses right mouse button down
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, IntPtr.Zero) 'Releases right mouse button
    
            count += 1
            If count = 10 Then Timer1.Stop() 'Stop the clicking process when clicked 10 times
        End Sub
    End Class
    EDIT: As The Thinker has said SendInput is the newer better way of doing what you want.


    • Edited by IronRazerz Monday, May 6, 2013 11:35 PM
    • Proposed as answer by The Thinker Tuesday, May 14, 2013 5:33 PM
    • Marked as answer by Youen Zen Tuesday, May 21, 2013 6:39 AM
    Monday, May 6, 2013 11:30 PM

All replies

  • hello

    i want to right click in another application i know the application name in process only

    i want to make many clicks by give it the X and Y where it will click every 3 seconds .

    thank you

    SendInput is the right api to use. maybe this thread will help you or point to another thread that will help:http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/7b9ac5e7-564e-410a-8840-dd5132ff36d9

     their are plenty of them on this vb.net forum you just have to search.

    I forgot use findwindow for finding that specific programs windows and bringing attention to it:

    http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/5352d3b8-4f9e-4a0a-8575-fdd592ae45f8

    The second thread looks similar to what you are trying to accomplish except they are using sendmessage.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.co.nr



    Monday, May 6, 2013 11:12 PM
  • Hi,

     I am not quite clear on what it is you want to do? Do you want to give it a list of X and Y positions and have it click the mouse on the next position in the list every 3 seconds or do you want to give it 1 position and click that position every 3 seconds? You will also need to think of when and how you want to tell your program to stop the mouse clicking process such as by pressing a hotkey, or after a certain number of clicks, or maybe after some other kind of event happens. Here is an example code to right click on one position of the screen every 3 seconds. I set it up to stop after 10 clicks because it can get a little tricky to close the program when the mouse keeps moving every 3 seconds. Thats why you need to figure how or when you want it to stop. I also included the Middle and Left mouse constants if you want to click a button other than the right.  :)

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Dim count As Integer = 0
        Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Const MOUSEEVENTF_LEFTUP As Int32 = &H4
        Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
        Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40
        Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
        Const MOUSEEVENTF_RIGHTUP As Int32 = &H10
    
        Private Declare Function mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As IntPtr) As Boolean
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Start the right mouse clicking timer
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Timer1.Interval = 100 Then
                Timer1.Stop()
                Timer1.Interval = 3000
                Timer1.Start()
            End If
    
            Cursor.Position = New Point(20, 37) 'Moves the mouse to this (X, Y) position
    
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, IntPtr.Zero) 'Presses right mouse button down
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, IntPtr.Zero) 'Releases right mouse button
    
            count += 1
            If count = 10 Then Timer1.Stop() 'Stop the clicking process when clicked 10 times
        End Sub
    End Class
    EDIT: As The Thinker has said SendInput is the newer better way of doing what you want.


    • Edited by IronRazerz Monday, May 6, 2013 11:35 PM
    • Proposed as answer by The Thinker Tuesday, May 14, 2013 5:33 PM
    • Marked as answer by Youen Zen Tuesday, May 21, 2013 6:39 AM
    Monday, May 6, 2013 11:30 PM