Search Tools Links Login

Tutorial: How to make a basic auto clicking program


This tutorial will teach the reader how to make a basic clicking program that will click on coordinates set by the user every 2 seconds.

Original Author: tazrockon

Code

How to make an Auto Clicker

By Tazrockon

Before you begin this tutorial there are some things I expect you to know.
You should have some experience with programming in Visual Basic and you should
at least have some basic knowledge of how to use Windows API in your programs.
I also expect you to be able to use forms, modules, and the standard Visual
Basic objects. Using this knowledge that already exists in your head I will
now attempt to teach you how to make a simple auto clicker that will repeatedly
click a certain coordinate on the screen every two seconds until told to stop.


The first thing you need to do is open up Visual Basic and start a Standard
EXE. On your form place two text boxes, two labels, and four command buttons.
Position the two text boxes side by side with some space in between in the middle
of the left side of the form. Above each text box put the two labels. Below
the two text boxes put two command buttons. Now, on the right side of the form
put the other two text boxes one above the other. In the label above the first
text box type in "X Pos:" (without the " 's). In the other label
type "Y Pos:". Clear out the text of the two text boxes and make the
first command button below them say "Lock" and the other "Unlock".
Now in the first command button on the right side of the form type in "Begin"
and in the one below it type "End". Now your GUI is pretty much finished.


Here's how the program is going to work when we are finished. When the form
loads, the first text box will contain the user's mouse's X coordinate and the
second will contain the user's Y coordinate as they move their mouse around
the screen. The first command button under the mouse position boxes that reads
"Lock" will be used to lock the users current mouse coordinates in
the text boxes so the user can move their mouse around without the text box's
numbers changing. The second command button which reads "Unlock" will
be used to unlock the current coordinates in the text boxes so that the user
can see their mouse's coordinates once again as they move their mouse across
the screen. The first command button on the right that reads "Begin"
will make the program start clicking the coordinates locked by the user and
the "End" button below it will make the program stop clicking.


Now the light reading is over and we are ready to get down to the code. Make
a new Module and in it add the lines:


Declare Function GetCursorPos& Lib "user32"
(lpPoint As PointAPI)

Type PointAPI

X As Long

Y As Long

End Type


Line1 : This is needed to tell the computer the program
wants to get the cursor position.

Line2 : This starts PointAPI.

Line3 : Sets the X coordinate variable as Long.

Line4 : Sets the Y coordinate variable as Long.

Line5 : This ends PointAPI.


Go back to your form and add a timer. Set Enabled to True and the Interval
to 10. Double click on the timer to get to the code window so you can make the
timer do something. In the timer sub type:


Dim pos

Dim pt As PointAPI

pos = GetCursorPos(pt)

Text1.Text = pt.X

Text2.Text = pt.Y


Line1 : This sets pos as a variable.

Line2 : This sets the variable pt to the PointAPI used in the module.

Line3 : Sets pos equal to GetCursorPos(pt). Basicly it gets the mouse coordinates
from the PointAPI.

Line4 : Makes Text1 read out the current X position of the mouse.

Line5 : Makes Text2 read out the current Y position of the mouse.


If you have done everything correctly you should now be able to run the program.
When it starts up it should tell the current position of your mouse as you move
it across the screen. Try moving your mouse to the very left bottom corner of
your computer screen and see what it says the coordinates are. Now we can add
the ability to Lock and Unlock coordinates. You will probably be suprised at
how easy this is to do. Go back to your form and double click on the button
that reads "Lock". In the command sub type:


Timer1.Enabled = False


Line1 : Disables Timer1 so that it will stop reading
out the mouse coordinates.


Go back to the form and double click on the button that reads "Unlock".
In this command sub in the code window type:


Timer1.Enabled = True


Line1 : Re-enables Timer1 so that it will start showing
the current coords again.


Now run the program. Test out clicking the Lock and Unlock buttons. Have you
found something bad about these buttons? Chances are you have. You can not lock
the coords of anywhere except where the Lock button is. This can easilly be
fixed. Go back to your form and click on the Lock button once. Now change the
caption from "Lock" to "&Lock". Notice how the button
now reads Lock. This means that when you run your program if you press Alt and
L on your keyboard your program will act as if you pressed the Lock button.
This will enable you to lock coordinates anywhere on your monitor.


We have set up the GUI, gotten the mouse's coordinates, and made Lock and Unlock
buttons. What are we going to do now? We are going to make the code that will
make our program actually click on the coordinates we lock. This is the hardest
part of the tutorial, but if you have made it this far alright and you have
gotten the code to get the cursor position to work, then you should be able
to achieve our new goal. Open up the module and add the following beneath End
Type of our Point API:


Declare Function SetCursorPos Lib "user32"
(ByVal X As Long, ByVal Y As Long) As Long

Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal
dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Public Const MOUSEEVENTF_LEFTDOWN = &H2

Public Const MOUSEEVENTF_LEFTUP = &H4


Line1 : This is needed to tell the computer what to use
to set the cursor position.

Line2 : Gets different mouse events from the computer.

Line3 : Makes the mouse event left down public so we can use it in the rest
of our module.

Line4 : Makes the mouse event left up public so we can use it in the rest of
our module.


Now we need to turn these API's into actions that are program can do. First
we will make the MouseMove action that we will use to, you got it, move the
mouse. Under the last line of API that we typed add the following code:


Sub MouseMove(xP As Long, yP As Long)

Dim move

move = SetCursorPos(xP, yP)

End Sub


Line1 : This creates the MouseMove Sub and sets the variable
xP and yP yo Long.

Line2 : Sets the variable move.

Line3 : Sets move equal to the API SetCursorPos in (xP,yP),

Line4 : Ends the Sub.


We have the code in the module to make our mouse move, but how do we incorporate
this into our form? Now the magic will begin. Go back to your form and add a
second timer. Set Enabled to False and make the Interval 2000 (every two seconds).
Double click on it and add this code:


Dim xP As Long

Dim yP As Long

xP = Text1.Text

yP = Text2.Text

MouseMove (xP), (yP)


Line1 : Sets the variable xP as Long

Line2 : Sets the variable yP as Long

Line3 : Makes xP equal the X position that's in the first text box

Line4 : Makes yP equal the Y position that's in the second text box

Line5 : Uses the MouseMove sub that we put in our module to move the mouse to
the locked coordinates (xP and yP)


Go back to your form. Double click on the button on the right side of the form
that reads "Begin". Here we will put in the code that enables our
second timer. Type in this code:


Timer2.Enabled = True


Line1 : This enables (turns on) Timer2 that has the MouseMove
procedure in it.


Now go back to your form and double click on the button that reads "End".
Here we will put the code that disables the second timer, which will make the
program stop trying to move the mouse to the locked coordinates.


Timer2.Enabled = False


Line1 : This disables Timer2 and will make the program
stop moving the mouse.


Now run the program. Position your mouse somewhere on the screen and lock its'
position. Click the begin button and watch your mouse move to the coordinates
you locked. Now quickly move your mouse over the Endd button and click on it.
This should make your program stop moving the mouse. All we have left to do
is make the program click on the coordinate. Go back to the module and add the
following code below our MouseMove sub:


Sub LeftClick(xP As Long, yP As Long)

mouse_event MOUSEEVENTF_LEFTDOWN, xP, yP, 0, 0

mouse_event MOUSEEVENTF_LEFTUP, xP, yP, 0, 0

End Sub


Line1 : This creates the LeftClick sub and sets the variables
xP and yP as Long.

Line2 : Tells our program to push down the left mouse button on the coordinates.

Line3 : Tells our program to let up the left mouse button on the coordinates.

Line4 : Ends the LeftClick sub.


Go back to the form and look at the Timer2 code. Below the MouseMove line we
need to add the code that will pull the LeftClick procedure from the module.
This is very easy. Add this code to the Timer2 sub:


LeftClick (xP), (yP)


Line1 : Uses the LeftClick sub that we put in our module
to left click the coordinates.


We should now have a fully functional automatic clicking program that will
click on given coordinates every two seconds until told to stop. Some things
to try are:


*Make a program that clicks on 2 or more points.

*Use a slider to allow the user to change the clicking interval.

*Make a mouse macro that will run Paint by clicking on Start, moving the mouse
up to Programs, move the mouse up to Accessories, and clicking on Paint.

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 84 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.