Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Mouse Operations

Rate me:
Please Sign up or sign in to vote.
3.52/5 (25 votes)
22 May 2007CPOL2 min read 53.5K   39   14
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:

C#
[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?

C#
[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.

C#
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:

C#
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:

C#
[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

C#
[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:

C#
using System.Runtime.InteropServices;

License

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


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

Comments and Discussions

 
GeneralSuper - Exatly what I needed. Pin
ZeedijkMike11-Jul-08 2:12
ZeedijkMike11-Jul-08 2:12 
I have a program I need to use every day.
But I also have to change some settings before using it. Instead of having to perform several clicks and selections I now can let a small application start up the program and change the settings without having to lift a finger (Except from starting the application that is...)
Great

ZeedijkMike

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.