Saturday, March 17, 2012

C# AutoClicker

Hello There!
Today we are going to program an AutoClicker.



What is an AutoClicker?
AutoClicker is a program that automatically clicks a mouse click for you.

Why AutoClicker?
- There are a lot of games (mostly online) that we need to click a lot and we don't have the energy to do it.
- This Could be a nice prank if that's your laugh.

OK, So Let's Start!
This Program will be written in C#.
If you don't have any C# Compiler you can download  Visual Studio Express Edition, I recommend 2008, but it's ok if you have any other version.
If we wan't to build a good AutoClicker we have to make it approachable from any program,
so we have to find a way to start the AutoClicking without having to focus our AutoClicker.
I have found a nice way to do this:
using RegisterHotKey function that is in the "user32.dll".
To take this function from the "user32.dll" we will use the following.
Right Click on the form -> View Code

Add
using System.Runtime.InteropServices; 
and copy this Code

[DllImport("user32.dll")];

public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); 


now we can use this function as we want.
This function registers a key, and when the key we registered is clicked it makes it go to our program.
In the program it will go to the "WndProc" function.(WndProc is the function that every time our form is being called it runs). 
Another Function we are going to need is:

[DllImport("user32.dll")];
  
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);


copy it under the RegisteHotKey function.
This function is the function that clicks for us.
In the WndProc we need to check what it was called for.
To see if it was called because of a hotkey we use a hotkey const.
(every event has it's code)

int WM_HOTKEY = 0x312;

if (m.Msg == WM_HOTKEY)
            
Thats the code for hotkey.
now to check what hot key was clicked we use:
 Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
------------------------------------------------------------------------------------------------
Ok, i'm going to leave my code here, and the whole project.
If you have any questions you can write a comment, and i'll answer Asap.

Code:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoClicker
{
    public partial class Form1 : Form
    {
        static Thread AutoClick;
        static int militime;
        public static int WM_HOTKEY = 0x312;

        [DllImport("user32.dll")]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void AClick()
        {
            while (true)
            {
                int x = Cursor.Position.X;
                int y = Cursor.Position.Y;
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                Thread.Sleep(militime);

            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AutoClick = new Thread(AClick);
            RegisterHotKey(this.Handle,(int)Keys.F1,0,(uint)Keys.F1);
            AutoClick.IsBackground = true;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_HOTKEY)
            {
                try
                {
                    militime = int.Parse(textBox1.Text);
                }
                catch
                {
                    militime = 0;
                }
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                if (key == Keys.F1)
                {
                    if (!AutoClick.IsAlive)
                    {
                        AutoClick.Start();
                        lstate.Text = "Clicking";
                        lstate.ForeColor = System.Drawing.Color.Green;
                        Controls.Add(lstate);
                    }
                    else
                    {
                        AutoClick.Abort();
                        AutoClick = new Thread(AClick);
                        lstate.Text = "Not Clicking";
                        lstate.ForeColor = System.Drawing.Color.Red;
                        Controls.Add(lstate);
                    }
                }
            }
            
        }

    }
}


Download The Project (rar file)

Important Notes:
-This program will not run on your computer if you don't have .Net support.
-If you have Windows that is a greater version than XP then you should have .Net support.
-Will not work on Mac or Linux without a special .Net support (if exists?)

Another Note:
if you want to activate the program without having to compile it yourself
(means open it as a .exe file and not as the project code itself)
Go to the Project Folder -> bin -> Debug -> AutoClicker.exe

5 comments:

  1. u made it auto clickin on Right Option
    [that open the opens u can do like refresh at Desktop]

    ReplyDelete
  2. as i put cordinates pls help send message : channel youtube

    darkstyle69booombang

    please help D:

    ReplyDelete
  3. This is a little older but thank you so much for this! Many auto clicker tutorials are for visual basic and not in c#. With this I added a minimum time and max time and made it choose and random point in between, to look less suspicious in games. Thanks!

    ReplyDelete
  4. Try changing the RegisterHotKey(this.Handle,(int)Keys.F1,0,(uint)Keys.F1); to RegisterHotKey(this.Handle,(int)Keys.F1,1,(uint)Keys.F1); , Reason Being is 0 is Right Click while 1 is Left Click

    ReplyDelete
  5. Thank you for the tips , you can take a look how i made auto clicker mac , it's pretty cool.

    ReplyDelete