Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Auto Clicker v1.0

3.44/5 (11 votes)
9 Aug 2007CPOL2 min read 1   11.6K  
A program that clicks your mouse automatically.

Screenshot - Auto_Clicker.jpg

Introduction

Auto Clicker is a program that clicks your mouse automatically. It allows the mouse to click without giving the user sore fingers. Just set the X-Y List using <SPACE> on your keyboard, set the click-interval, click the <start> button, and away from mouse and keyboard, it goes clicking the mouse automatically. For getting a faster auto-click speed, try setting the click interval to a lower number (milliseconds). To stop Auto Clicker, just press <ESC> on the keyboard. Auto Clicker can be used to make repetitive clicking easy, but should not be used to cheat in online-games. Auto Clicker is a freeware, easy to use, intuitive program, and the program source is open.

Using the Code

To build this program, you should know the mouse_event() function and hooking mechanism. From MSDN, you can know more about how the mouse_event() function synthesizes mouse motion and button clicks.

These are examples of using mouse_event() in this program:

C++
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Left Key Down
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()); // Left Key UP

mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Right Key Down
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, GetMessageExtraInfo()); // Right Key Up

To get keyboard messages when the application loses focus, you need hooking procedures.

In OnInitDialog(), SetWindowsHookEx() is used, and HookProc of SetWindowsHookEx is used as follows:

C++
LRESULT CALLBACK HookMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND hwnd;
    LRESULT lResult = 0;
    if(nCode == HC_ACTION) 
    {
        if(((EVENTMSG*)lParam)->message == VK_ESCAPE)
        {
            hwnd= ::FindWindow(NULL, strProgName);
            ::SendMessage(hwnd,WM_KEY_ESCAPE,1,1);
            return 1; 
        }
        else if(((EVENTMSG*)lParam)->message == VK_PAUSE)
        {
            hwnd=::FindWindow(NULL, strProgName);
            ::SendMessage(hwnd,WM_KEY_PAUSE,1,1);
            return 1; 
        }
    }

    return CallNextHookEx( hHook, nCode, wParam, lParam);
}

How to Use

First of all, you should fill the X-Y List box with the mouse points on the screen. Just move your mouse while pressing the <SPACE> key on the keyboard. While the Auto Clicker is working, if you want to stop it, just press the <ESC> key on the keyboard. If the [PAUSE] key on the keyboard is pressed, you can pause Auto Clicker. You can save the X-Y List to a file and later load it from the file using the <save> and <load> buttons.

Points of Interest

Does anybody know how to build this program without using the hooking mechanism?

History

This is my first article while I working for http://www.softahead.com. My latest article is available here.

License

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