Click here to Skip to main content
15,039,359 members
Articles / Programming Languages / C#
Posted 15 Jan 2009

Tagged as


94 bookmarked

Auto Clicker C#

Rate me:
Please Sign up or sign in to vote.
4.56/5 (32 votes)
19 Apr 2009CPOL2 min read
A program that simulates mouse and keyboard


This program simulates your mouse and keyboard; user simulates step by step mouse or keyboard actions; when user presses the “start” button, the actions run automatically.


The reason that I built this code is to save me a lot of time. When I test my code (Windows application most of the time), I need to run the application and do the same step over and over again. All of the steps are by mouse and keyboard; some of them take a few seconds, others take more. So I built a program that simulates my mouse and keyboard.

How To Use

First load actions.

To simulate mouse:

  1. Move mouse to specific location and:
  2. Press ‘c’ to left click (mouse)
  3. Press ‘d’ to double click (mouse)
  4. Press ‘r’ to right click (mouse)

To simulate keyboard:

  1. Fill SendKeys command in text-box
  2. Simulate click to specific location
  3. Press ‘t’ (keyboard)

To delete or edit action use context-menu. You can move mouse out of the main window and simulate action. The biggest problem is to stay focused on the main window without moving the mouse -> the solution is very simple ‘Alt + Tab’ (until you refocus the main window). After you load your action, press “Start” button and wait (don't move your mouse, until the loop finished. You can save or load (open) configuration.

Using the Code

I tried to make the code as simple as possible.

The Next API Moves the Mouse to the Relevant Position

public static extern int SetCursorPos(int x, int y);

The next API simulates mouse events (left/right click down/up, wheel EXE):

private const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */
private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */

    CharSet = CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons,
    int dwExtraInfo);

To simulate one click, you need to use this method twice: the first time for down click and the second time for up click. On the save side, wait 100 milliseconds between moving new location and the click, like in the following code:

SetCursorPos(action.X, action.Y)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

Points of Interest

I ran actions loop in thread. Because it’s wrong to turn to the main thread from another thread, I use SynchronizationContext class. I will not expand on this issue in this article although it’s a great solution to fixed cross thread problem.


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


About the Author

Avi Bar
Israel Israel
No Biography provided

Comments and Discussions

Questionhow can I make it click enter? Pin
Member 1276920930-Sep-16 5:49
MemberMember 1276920930-Sep-16 5:49 
Questionhow to make the program click enter? Pin
Member 1276920930-Sep-16 5:49
MemberMember 1276920930-Sep-16 5:49 
QuestionRepeat Pin
Member 123008501-Feb-16 8:49
MemberMember 123008501-Feb-16 8:49 
GeneralMy vote of 5 Pin
Sharad Syn11-Sep-15 1:54
MemberSharad Syn11-Sep-15 1:54 
Questionlooping the Clicking Software Pin
armanoo14-Aug-15 6:29
Memberarmanoo14-Aug-15 6:29 
Generalapp to record Test case and record mouse movement and keyboard clicks Pin
Member 117448536-Jun-15 20:31
MemberMember 117448536-Jun-15 20:31 
SuggestionRepeat functionality? Pin
Member 1130468211-Dec-14 19:18
MemberMember 1130468211-Dec-14 19:18 
QuestionThanks Pin
Member 1125449421-Nov-14 18:52
MemberMember 1125449421-Nov-14 18:52 
GeneralThis is awesome! Pin
Member 1122587711-Nov-14 15:19
MemberMember 1122587711-Nov-14 15:19 
GeneralVery much useful... Pin
geamdJOB28-May-14 2:23
MembergeamdJOB28-May-14 2:23 
GeneralMy vote of 4 Pin
ThiyagarajanR17-Apr-14 8:14
MemberThiyagarajanR17-Apr-14 8:14 
very much useful...
GeneralNot a True Auto Clicker Pin
User 965661011-Feb-14 4:23
MemberUser 965661011-Feb-14 4:23 
Ivandro Ismael17-Jan-14 13:36
MemberIvandro Ismael17-Jan-14 13:36 
QuestionFree Auto Clicker Software Pin
frequent17-Jan-13 22:23
Memberfrequent17-Jan-13 22:23 
QuestionFreundin nur DANKE! Pin
PAUL52721-Jul-12 12:29
MemberPAUL52721-Jul-12 12:29 
Questionpossibility to cancel /close Pin
Walsumer5-Mar-12 23:50
MemberWalsumer5-Mar-12 23:50 
GeneralMy vote of 4 Pin
rama charan9-Feb-12 2:03
Memberrama charan9-Feb-12 2:03 
QuestionTyping support Pin
Cool Smith30-Dec-11 9:38
MemberCool Smith30-Dec-11 9:38 
GeneralMy vote of 4 Pin
HotBB13-Dec-11 13:14
MemberHotBB13-Dec-11 13:14 
GeneralMy vote of 5 Pin
Azagatoth10-Dec-10 1:40
MemberAzagatoth10-Dec-10 1:40 
QuestionHow to make Return command to the first step after the end step finished Pin
firman arrow17-Jul-10 16:29
Memberfirman arrow17-Jul-10 16:29 
Generaldoesn't open a configuration Pin
tanya1114-Apr-10 3:52
Membertanya1114-Apr-10 3:52 
Questionhey, could you help me? Pin
bill8989127-Mar-10 21:59
Memberbill8989127-Mar-10 21:59 
GeneralHI~~ Pin
ji houng suk24-Jun-09 3:07
Memberji houng suk24-Jun-09 3:07 
Generalok article [modified] Pin
Donsw18-May-09 7:25
MemberDonsw18-May-09 7:25 

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

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