Click here to Skip to main content
15,121,414 members
Articles / Programming Languages / C#
Posted 10 Mar 2007


39 bookmarked

Mouse Operations

Rate me:
Please Sign up or sign in to vote.
3.56/5 (24 votes)
22 May 2007CPOL2 min read
Doing some things to the mouse using API


It was a dark and stormy night... I was sitting next to my computer reading about a virus that kept the mouse cursor at the top-left part of the screen and clicking. I thought to myself "Wow, I wonder how it's done", and so I started to investigate.

Unfortunately, those types of actions (such as simulating clicks) aren't really well documented (or incorporated into .NET Frameworks), and there aren't really any guides on that issue, so after collecting data from over a dozen sites I decided to make this guide.

The Code

Ok, first I'll describe how the program works:

It moves the mouse to the top-left corner of the screen (0,0) and clicks the mouse (causing any other program to lose focus).

The following API moves the mouse:

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

This is fairly easy to understand, it gets the coordinates and moves the mouse to that position, but how do I click then?

public static extern void mouse_event
	(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

Ok then, there's an API called mouse_event. you're probably wondering how it's used, so here's an example.

First, you make an enumeration (you might as well use the integers themselves, I just find this way to be more organized.

public enum MouseEventType : int
    LeftDown = 0x02,
    LeftUp = 0x04,
    RightDown = 0x08,
    RightUp = 0x10

Now, what exactly is a mouse click? It's a click down and then up. Only sending LeftDown will be like a mouse hold, and only sending LeftUp... will just be weird, that's why in order to emulate a mouse click, you have to send both:

mouse_event((int)MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
mouse_event((int)MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);

Woohoo! We got ourselves a Mouseclick!

The full code is as follows:

public static extern void mouse_event
	(MouseEventType dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public static extern int SetCursorPos(int x, int y);

public enum MouseEventType : int
    LeftDown = 0x02,
    LeftUp = 0x04,
    RightDown = 0x08,
    RightUp = 0x10

private void button1_Click(object sender, EventArgs e)
    SetCursorPos(0, 0);
    mouse_event(MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
    mouse_event(MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);

By putting the click in a function and calling it with a timer (with a 1 millisecond interval, for example), you'll be making the exact same virus (but viruses are don't).

Other Functions

Well, there is one more thing I'd like to show you, which is a really weird function I found that for some unknown reason exists, and it is SwapMouseButton. It does exactly what the name suggests - swaps the mouse buttons (left = right, right = left).

How To Use

static extern bool SwapMouseButton(bool fSwap);

private void button1_Click(object sender, EventArgs e)

Sending true to the function will swap the mouse buttons, sending false will turn them back to normal.

Final Notes

Well, this is my first article here (and at all), hope it helps.

Oh, and don't forget adding the following to your using clause:

using System.Runtime.InteropServices;


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


About the Author

I'm a 16 year old guy, studying at some high-school in Israel.

Comments and Discussions

Questionnot working now Pin
Omarkkk24-Feb-15 21:28
MemberOmarkkk24-Feb-15 21:28 
GeneralCompletely awesome Pin
peter kaptein5-May-11 10:07
Memberpeter kaptein5-May-11 10:07 
GeneralSuper - Exatly what I needed. Pin
ZeedijkMike11-Jul-08 3:12
MemberZeedijkMike11-Jul-08 3:12 
GeneralSwapMouseButton Function Pin
DArouca26-Nov-07 23:56
MemberDArouca26-Nov-07 23:56 
GeneralThanks. Pin
zam66422-May-07 11:18
Memberzam66422-May-07 11:18 
GeneralVery good Pin
szolDat5-May-07 12:41
MemberszolDat5-May-07 12:41 
GeneralRe: Very good Pin
sharpiesharpie11-Jun-07 6:03
Membersharpiesharpie11-Jun-07 6:03 
General[DllImport("user32.dll")] Pin
fire storm29-Mar-07 2:47
Memberfire storm29-Mar-07 2:47 
GeneralRe: [DllImport("user32.dll")] Pin
fire storm29-Mar-07 3:06
Memberfire storm29-Mar-07 3:06 
GeneralWhy don't you use System.Windows.Forms.Cursor Pin
Joseph103026-Mar-07 22:51
professionalJoseph103026-Mar-07 22:51 
GeneralRe: Why don't you use System.Windows.Forms.Cursor Pin
sharpiesharpie6-Apr-07 8:09
Membersharpiesharpie6-Apr-07 8:09 
GeneralRe: Why don't you use System.Windows.Forms.Cursor Pin
fwsouthern22-May-07 16:45
Memberfwsouthern22-May-07 16:45 
Generalgood work Pin
Martin Garmendia10-Mar-07 13:23
MemberMartin Garmendia10-Mar-07 13:23 
GeneralRe: good work Pin
Skippums14-Nov-07 20:45
MemberSkippums14-Nov-07 20:45 

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.