Click here to Skip to main content
15,884,873 members
Articles / Desktop Programming / Win32
Article

Global Mouse and Keyboard Library

Rate me:
Please Sign up or sign in to vote.
4.94/5 (132 votes)
8 Aug 2008CPOL3 min read 542.7K   27.3K   322   206
Easy-to-use library with global mouse and keyboard hooks and simulators.

Image 1

Introduction

This article explains how to use the mouse and keyboard library that I have created. This library consists of two things: global mouse and keyboard hooks, and global mouse and keyboard simulators.

The global hooks contain a set of events that follow the .NET event model, so they should be very simple to use if you've done anything with events before.

The simulators will actually simulate mouse movements, clicks, keyboard presses, etc. This can be useful for macro recording (which is one of the sample projects), and of course, messing with your friends. :)

Background

I've found a lot of hook and simulator code out there, but a lot of it was not very organized, and a bit hard to use. The goal here is to make a very simple and easy-to-use library for mouse and keyboard operations not natively supported by .NET.

Global Hooks

This section will explain how to use the global hooks which can capture mouse and keyboard events from any application. These events are very similar to the ones that appear on Windows controls, so they should look familiar.

Using the mouse hook:

C#
// Create the mouse hook
MouseHook mouseHook = new MouseHook();

// Capture the events
mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel); 

// Start watching for mouse events
mouseHook.Start();

...

// Stop watching (don't forget to do this before closing application!)
mouseHook.Stop();

Using the keyboard hook:

C#
// Create the keyboard hook
KeyboardHook keyboardHook = new KeyboardHook();

// Capture the events
keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);

// Start watching for keyboard events
keyboardHook.Start();

...

// Stop watching (don't forget to do this before closing application!)
keyboardHook.Stop();

Note: When you are setting the events, Visual Studio will name and create a blank method for you. You only need to type this much on the event ...

C#
keyboardHook.KeyDown +=

...and then hit TAB two times. Visual Studio will finish the rest of the line, and will go out and create the blank method for you. This is a nice feature, and saves a lot of time; use it!

Simulators

This section will explain how to use the the mouse and keyboard simulators to simulate mouse clicks and keyboard key presses. Both the KeyboardSimulator and MouseSimulator classes are static, so they are pretty simple to use.

Simulating mouse events:

C#
// Press Left Mouse Button Down
MouseSimulator.MouseDown(MouseButton.Left);

// Let Left Mouse Button Up
MouseSimulator.MouseUp(MouseButton.Left);

// Press down and Let up Left Mouse Button
// (equivalent to two lines above)
MouseSimulator.Click(MouseButton.Left);

// Double click Left Mouse button
// (equivalent to two Click()s above)
MouseSimulator.DoubleClick(MouseButton.Left);

The code above is used to simulate pressing and letting up a certain mouse button. The one parameter is just an enumeration of the three possible mouse buttons: Left, Right, and Middle.

Moving the mouse position:

C#
// Move mouse cursor to Top Left of screen
MouseSimulator.X = 0;
MouseSimulator.Y = 0;

// Move the mouse cursor to the right by 20 pixels
MouseSimulator.X += 20;

The X and Y above are properties. You can use them to get the current position of the mouse cursor, or you can set them to move the mouse cursor to a new location.

Keyboard simulators:

C#
// Press the A Key Down
KeyboardSimulator.KeyDown(Keys.A);

// Let the A Key back up
KeyboardSimulator.KeyUp(Keys.A);

// Press A down, and let up (same as two above)
KeyboardSimulator.KeyPress(Keys.A);

The code above will simulate keyboard key presses. You can press a key down (first line), which doesn't let it up yet. The second line, KeyUp, will release a key that has been pressed down, the third line with do both steps in one shot.

I also included some standard keyboard shortcuts. These can all be done with the code above, but it simplifies it a bit, and makes the code a bit more readable and obvious.

C#
// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);

// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);

The code above does the exact same thing twice, except the first is a bit shorter and more obvious.

A Sample Application: Global Macro Recorder

Image 2

A macro recorder is a great example for this library, since we can use the hooks to record the macro, and the simulators to play back.

Check out the Macro project in the downloadable source code.

Future Additions / Revisions

I'm going to edit and add to this library over time as I get feedback. If you think something should be added, changed, or find a problem, please post in the comments section, and I'll do what I can. Thanks. :)

Revision History

  • 7/23/08: Released first version in three versions of .NET.
  • 7/28/08: Added the Application.ApplicationExit event to make sure hooks stop.
  • 8/8/08: Got rid of the debug code in the Macro example, added the MouseWheel method to the MouseSimulator class, and got rid of the duplicate MousePoint class used in MouseSimulator.

License

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


Written By
Software Developer GEA Refrigeration
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan you get the windows msg when the frontwindow changed? Pin
phiree28-Jul-08 14:58
phiree28-Jul-08 14:58 
AnswerRe: can you get the windows msg when the frontwindow changed? Pin
Brian Geiman29-Jul-08 8:23
Brian Geiman29-Jul-08 8:23 
GeneralRe: can you get the windows msg when the frontwindow changed? Pin
phiree26-Sep-11 18:20
phiree26-Sep-11 18:20 
QuestionCan it? Pin
Silvyster28-Jul-08 14:10
Silvyster28-Jul-08 14:10 
AnswerRe: Can it? Pin
Brian Geiman28-Jul-08 14:15
Brian Geiman28-Jul-08 14:15 
QuestionCleanup Pin
Richarmeleon28-Jul-08 10:47
Richarmeleon28-Jul-08 10:47 
AnswerRe: Cleanup Pin
Brian Geiman28-Jul-08 12:59
Brian Geiman28-Jul-08 12:59 
GeneralRe: Cleanup Pin
The_Mega_ZZTer8-Aug-08 18:12
The_Mega_ZZTer8-Aug-08 18:12 
Even better, have the hook stop itself in the class destructor.

The hook will then stop if the program ends automatically, or if the hook object goes out of scope and can no longer be used or trigger events by the program (thus making it useless anyway). Right now your objects will stay in memory until the program ends (see last paragraph, it's because you add an event handler for an Application event, and Application never goes out of scope), never going out of scope and getting cleaned up. This is often mistaken for a memory leak problem (I think some of the guys who did the DARPA robot race thing claimed .NET had a memory leak when they simply misunderstood how garbage collection worked in .NET and thus programmed a memory eater).

Of course if an app crashes the hook is never released then, with both our solutions. I would try handling Application.UnhandledException or whatever it's called to take care of that case (I think you can throw the exception again from there to get the normal crash dialog, or maybe the next handler? I forget, don't take my word for it, RTM first).

Also note that if the user creates an object from your class, adds handlers to its events, and then lets the object fall out of scope, the destructor won't be called and the object won't be cleaned up until the event handlers are unassigned (usually when the program ends). So there shouldn't be any compatibility issues to worry about by allowing the destructor to remove the hook.
GeneralCool! Pin
gore0124-Jul-08 19:45
gore0124-Jul-08 19:45 
GeneralRe: Cool! Pin
Brian Geiman27-Jul-08 10:25
Brian Geiman27-Jul-08 10:25 
GeneralRe: Cool! Pin
Don G28-Jul-08 7:19
Don G28-Jul-08 7:19 
QuestionSuppressing Mouse Click Pin
wbmstr2good24-Jul-08 8:46
wbmstr2good24-Jul-08 8:46 
AnswerRe: Suppressing Mouse Click Pin
Brian Geiman24-Jul-08 12:56
Brian Geiman24-Jul-08 12:56 
GeneralRe: Suppressing Mouse Click Pin
AnandChavali24-Jul-08 21:04
AnandChavali24-Jul-08 21:04 
GeneralRe: Suppressing Mouse Click Pin
Brian Geiman25-Jul-08 2:35
Brian Geiman25-Jul-08 2:35 
GeneralGreat Article!! Pin
J a a n s23-Jul-08 23:00
professionalJ a a n s23-Jul-08 23:00 
GeneralRe: Great Article!! Pin
Brian Geiman24-Jul-08 3:07
Brian Geiman24-Jul-08 3:07 

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.