Click here to Skip to main content
12,451,202 members (54,016 online)
Click here to Skip to main content
Add your own
alternative version

Stats

37.5K views
37 bookmarked
Posted

Mouse Operations

, 22 May 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionnot working now Pin
Member 1116931224-Feb-15 20:28
memberMember 1116931224-Feb-15 20:28 
GeneralCompletely awesome Pin
peter kaptein5-May-11 9:07
memberpeter kaptein5-May-11 9:07 
GeneralSuper - Exatly what I needed. Pin
ZeedijkMike11-Jul-08 2:12
memberZeedijkMike11-Jul-08 2:12 
GeneralSwapMouseButton Function Pin
DArouca26-Nov-07 22:56
memberDArouca26-Nov-07 22:56 
GeneralThanks. Pin
zam66422-May-07 10:18
memberzam66422-May-07 10:18 
GeneralVery good Pin
szolDat5-May-07 11:41
memberszolDat5-May-07 11:41 
GeneralRe: Very good Pin
sharpiesharpie11-Jun-07 5:03
membersharpiesharpie11-Jun-07 5:03 
General[DllImport("user32.dll")] Pin
fire storm29-Mar-07 1:47
memberfire storm29-Mar-07 1:47 
GeneralRe: [DllImport("user32.dll")] Pin
fire storm29-Mar-07 2:06
memberfire storm29-Mar-07 2:06 
GeneralWhy don't you use System.Windows.Forms.Cursor Pin
Joseph103026-Mar-07 21:51
memberJoseph103026-Mar-07 21:51 
GeneralRe: Why don't you use System.Windows.Forms.Cursor Pin
sharpiesharpie6-Apr-07 7:09
membersharpiesharpie6-Apr-07 7:09 
GeneralRe: Why don't you use System.Windows.Forms.Cursor Pin
fwsouthern22-May-07 15:45
memberfwsouthern22-May-07 15:45 
Generalgood work Pin
mattinsalto10-Mar-07 12:23
membermattinsalto10-Mar-07 12:23 
GeneralRe: good work Pin
Skippums14-Nov-07 19:45
memberSkippums14-Nov-07 19: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 22 May 2007
Article Copyright 2007 by sharpiesharpie
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid