Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Mouse Operations

0.00/5 (No votes)
22 May 2007 1  
Doing some things to the mouse using API

Introduction

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:

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

[DllImport("user32.dll")]
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:

[DllImport("user32.dll")]
public static extern void mouse_event
	(MouseEventType dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32")]
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 bad...so 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

[DllImport("user32.dll")]
static extern bool SwapMouseButton(bool fSwap);

private void button1_Click(object sender, EventArgs e)
{
    SwapMouseButton(true);
}

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;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here