Click here to Skip to main content
Click here to Skip to main content

Tagged as

Auto Clicker C#

, 19 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A program that simulates mouse and keyboard
ClickerEXE

Introduction

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.

Background

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

[DllImport("user32")]
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 */

DllImport("user32.dll",
    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)
Thread.Sleep(100);
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.

License

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

Share

About the Author

Avi Bar

Israel Israel
No Biography provided

Comments and Discussions

 
GeneralThis is awesome! PinmemberMember 1122587711-Nov-14 16:19 
SuggestionMessage Automatically Removed PinmemberMember 1104824331-Aug-14 1:24 
Generalutility to record mouse and keyboard events PinmemberMember 1096512422-Jul-14 22:36 
GeneralVery much useful... PinmembergeamdJOB28-May-14 3:23 
GeneralMy vote of 4 PinmemberThiyagarajanR17-Apr-14 9:14 
GeneralNot a True Auto Clicker PinmemberArithForU111-Feb-14 5:23 
QuestionOUTSIDE FORM CLICK PinmemberIvandro Ismael17-Jan-14 14:36 
QuestionFree Auto Clicker Software Pinmemberfrequent17-Jan-13 23:23 
QuestionFreundin nur DANKE! Pinmemberpredador802821-Jul-12 13:29 
Questionpossibility to cancel /close PinmemberWalsumer6-Mar-12 0:50 
GeneralMy vote of 4 [modified] Pinmemberrama charan9-Feb-12 3:03 
QuestionTyping support PinmemberCool Smith30-Dec-11 10:38 
GeneralMy vote of 4 PinmemberHotBB13-Dec-11 14:14 
GeneralMy vote of 5 PinmemberAzagatoth10-Dec-10 2:40 
QuestionHow to make Return command to the first step after the end step finished Pinmemberfirman arrow17-Jul-10 17:29 
Generaldoesn't open a configuration Pinmembertanya1114-Apr-10 4:52 
Questionhey, could you help me? Pinmemberbill8989127-Mar-10 22:59 
GeneralHI~~ Pinmemberji houng suk24-Jun-09 4:07 
Generalok article [modified] PinmemberDonsw18-May-09 8:25 
GeneralRe: ok article Pinmemberpoteb5-Feb-10 4:31 

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
Web01 | 2.8.1411019.1 | Last Updated 20 Apr 2009
Article Copyright 2009 by Avi Bar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid