Click here to Skip to main content
11,576,692 members (59,863 online)
Click here to Skip to main content

Creating a Simple Autoclicker

, 30 Aug 2006 CPOL 133.4K 7.1K 71
Rate this:
Please Sign up or sign in to vote.
Let's create software that clicks for us
Sample Image - autoclicker.png

Introduction

There are some computer games or applications where you need to repeatedly click on the same place on the screen many times. Imagine a game where you chop down a tree by clicking on it. Then a next tree appears on the same spot, so after a while, you need to click there again.

As we are human beings, we like to simplify boring tasks. As programmers, we can simplify this task by creating software to do it for us.

The Coding

What do we need to do in the autoclicker? To keep things simple, we only set two things: where to click and how often to click. The point where to click can be stored in a variable of type Point, the interval will be set on our Timer.

The first thing to do is to create a new Windows Form. Then we add our variable to hold the click location.

//this will hold the location where to click
Point clickLocation = new Point(0,0);

Next, we need to set the location for our clicking. One of the ways to do this is to start a countdown. While it is running, the user can point the mouse to the desired position and after the countdown ends, we get its coordinates.

We can do this by activating appropriately long timeout (for example 5 seconds) and then collecting the mouse location. So, we can add a button with following OnClick event handler:

private void btnSetPoint_Click(object sender, EventArgs e)
{
   timerPoint.Interval = 5000;
   timerPoint.Start();
}

We also create a timer to help us get the mouse location. We can get the mouse location from the Position property of the Cursor class. The set location can be displayed for example on the window title.

So, let's create the second timer and add the following Tick handler to it:

private void timerPoint_Tick(object sender, EventArgs e)
{
    clickLocation = Cursor.Position;
    //show the location on window title
    this.Text = "autoclicker " + clickLocation.ToString();
    timerPoint.Stop();
}

Now we want to set the main timer interval (we can use the NumericUpDown control). Remember that the lower bound of its interval should be greater than zero, as we cannot set the timer interval to zero milliseconds.

Now to the clicking itself. In each time our main timer elapses (add another Timer control to the form), we want to click on a specified position. We cannot do this in pure managed code, we must use the import method SendInput of the user32.dll library. We will use it to synthesize the mouse click. Don't forget to place using System.Runtime.InteropServices; to the beginning of your code when you use DllImport.

[DllImport("User32.dll", SetLastError = true)]
public static extern int SendInput(int nInputs, ref INPUT pInputs, 
                                   int cbSize);

The method SendInput uses three parameters:

  • nInputs specifies how many structures pInputs points to
  • pInputs is a reference to an array of INPUT structures
  • cbSize is the size of INPUT structure

To keep things simple, we will use only one INPUT structure. We will need to define this structure and some constants too:

//mouse event constants
const int MOUSEEVENTF_LEFTDOWN = 2;
const int MOUSEEVENTF_LEFTUP = 4;
//input type constant
const int INPUT_MOUSE = 0;

public struct MOUSEINPUT
{
    public int dx;
    public int dy;
    public int mouseData;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
}

public struct INPUT
{
    public uint type;
    public MOUSEINPUT mi;
};

Each time our timer elapses, we want to click. We do it by moving the cursor to the memorized place, then setting up the INPUT structure and filling it with required values. Each click consists of pressing mouse button and releasing mouse button, so we need to send two messages - one for button down (press) and another for button up (release).

So add a handle to the Tick event of the main timer with this code:

private void timer1_Tick(object sender, EventArgs e)
{
    //set cursor position to memorized location
    Cursor.Position = clickLocation;
    //set up the INPUT struct and fill it for the mouse down
    INPUT i = new INPUT();
    i.type = INPUT_MOUSE;
    i.mi.dx = 0;
    i.mi.dy = 0;
    i.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    i.mi.dwExtraInfo = IntPtr.Zero;
    i.mi.mouseData = 0;
    i.mi.time = 0;
    //send the input 
    SendInput(1, ref i, Marshal.SizeOf(i));
    //set the INPUT for mouse up and send it
    i.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1, ref i, Marshal.SizeOf(i));
}

Finally - we can use a button to start/stop the autoclicking feature. Let's create a button with the following handler:

private void btnStart_Click(object sender, EventArgs e)
{
    timer1.Interval = (int)numericUpDown1.Value;
    if (!timer1.Enabled)
    {
        timer1.Start();
        this.Text = "autoclicker - started";
    }
    else
    {
        timer1.Stop();
        this.Text = "autoclicker - stopped";
    }
}

Further Steps

That's it. It's quite simple. How can we further improve this program?

We could...

  • use random intervals of clicking
  • add random movements with the mouse, then return to the point where we click
  • move the mouse while "holding" down the button
  • capture what is displayed on the screen and click on a specific colour
  • click on sequence of points (create some kind of macros)

History

  • 30th August, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Juraj Borza
Software Developer MicroStep
Slovakia Slovakia
Juraj Borza is currently working as a full-time C# developer located in Bratislava, Slovakia. He likes coding for fun and releases some projects under open-source license.

You may also be interested in...

Comments and Discussions

 
QuestionQuestion Pin
Member 1176371113-Jun-15 8:46
memberMember 1176371113-Jun-15 8:46 
QuestionAutoclicker Pin
Member 111231111-Oct-14 2:51
memberMember 111231111-Oct-14 2:51 
QuestionCan you help me?? Pin
Member 963288530-Mar-13 2:40
memberMember 963288530-Mar-13 2:40 
Generalplease help Pin
Ahmed12726-Apr-12 10:14
memberAhmed12726-Apr-12 10:14 
SuggestionRe: please help Pin
ismamad11-Dec-12 8:10
memberismamad11-Dec-12 8:10 
GeneralMy vote of 1 Pin
likenload6-Dec-11 14:52
memberlikenload6-Dec-11 14:52 
QuestionAwesome article! Pin
nanomass29-Aug-11 3:53
membernanomass29-Aug-11 3:53 
GeneralGreat article Pin
wisodev13-Feb-10 22:59
memberwisodev13-Feb-10 22:59 
QuestionDoes it work on DirectX game? Pin
Member 221126817-Apr-09 3:20
memberMember 221126817-Apr-09 3:20 
QuestionMultiple clicks/timers Pin
Steve Farmer26-Mar-09 7:46
memberSteve Farmer26-Mar-09 7:46 
AnswerRe: Multiple clicks/timers Pin
Juraj Borza26-Mar-09 20:21
memberJuraj Borza26-Mar-09 20:21 
QuestionSendInput Pin
danzar29-Oct-07 19:43
memberdanzar29-Oct-07 19:43 
QuestionGlobal key recognition Pin
Trinith28-Aug-07 20:00
memberTrinith28-Aug-07 20:00 
AnswerRe: Global key recognition Pin
Juraj Borza9-Oct-07 9:37
memberJuraj Borza9-Oct-07 9:37 
GeneralMore options [modified] Pin
ratzz13-Jul-07 5:39
memberratzz13-Jul-07 5:39 
GeneralRe: More options Pin
Juraj Borza30-Jul-07 23:15
memberJuraj Borza30-Jul-07 23:15 
QuestionHow do you start it? Pin
wjgchin9-Jul-07 8:54
memberwjgchin9-Jul-07 8:54 
AnswerRe: How do you start it? Pin
Juraj Borza10-Jul-07 7:51
memberJuraj Borza10-Jul-07 7:51 
GeneralF1, F2 = start, stop HOW???? [modified] Pin
sweepslove23-Apr-07 1:30
membersweepslove23-Apr-07 1:30 
GeneralRe: F1, F2 = start, stop HOW???? Pin
Juraj Borza23-Apr-07 1:33
memberJuraj Borza23-Apr-07 1:33 
GeneralGreat Article ! Pin
MTCoder10-Feb-07 1:26
memberMTCoder10-Feb-07 1:26 
QuestionHOW DO I GET WINDOWS FORMS?!?!? Pin
Member #37942004-Feb-07 9:56
memberMember #37942004-Feb-07 9:56 
AnswerRe: HOW DO I GET WINDOWS FORMS?!?!? Pin
Christian Graus4-Feb-07 10:16
staffChristian Graus4-Feb-07 10:16 
AnswerRe: HOW DO I GET WINDOWS FORMS?!?!? Pin
ismamad11-Dec-12 8:11
memberismamad11-Dec-12 8:11 
GeneralNice program Pin
Brett Clark27-Jan-07 13:41
memberBrett Clark27-Jan-07 13:41 
GeneralRe: Nice program Pin
Juraj Borza4-Feb-07 22:33
memberJuraj Borza4-Feb-07 22:33 
GeneralNice Util Pin
nightshade_6923-Jan-07 8:16
membernightshade_6923-Jan-07 8:16 
GeneralRe: Nice Util Pin
Juraj Borza23-Apr-07 1:32
memberJuraj Borza23-Apr-07 1:32 
GeneralI Mailed you about a request for a autoclicker. Pin
Chazzz7-Nov-06 1:01
memberChazzz7-Nov-06 1:01 
GeneralFrustrated Pin
Aeriant27-Oct-06 14:39
memberAeriant27-Oct-06 14:39 
GeneralRe: Frustrated Pin
Juraj Borza2-Nov-06 22:59
memberJuraj Borza2-Nov-06 22:59 
QuestionWhere's the code ? Pin
Ramene Anthony30-Aug-06 4:49
memberRamene Anthony30-Aug-06 4:49 
AnswerRe: Where's the code ? Pin
Juraj Borza30-Aug-06 5:00
memberJuraj Borza30-Aug-06 5:00 
GeneralRe: Where's the code ? Pin
okinoob2-Nov-06 9:18
memberokinoob2-Nov-06 9:18 
GeneralRe: Where's the code ? Pin
Juraj Borza2-Nov-06 22:57
memberJuraj Borza2-Nov-06 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 30 Aug 2006
Article Copyright 2006 by Juraj Borza
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid