Click here to Skip to main content
13,866,652 members
Click here to Skip to main content
Add your own
alternative version


314 bookmarked
Posted 23 Jul 2008
Licenced CPOL

Global Mouse and Keyboard Library

, 8 Aug 2008
Rate this:
Please Sign up or sign in to vote.
Easy-to-use library with global mouse and keyboard hooks and simulators.


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. :)


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:

// 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


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

Using the keyboard hook:

// 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


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

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 ...

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!


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:

// Press Left Mouse Button Down

// Let Left Mouse Button Up

// Press down and Let up Left Mouse Button
// (equivalent to two lines above)

// Double click Left Mouse button
// (equivalent to two Click()s above)

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:

// 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:

// Press the A Key Down

// Let the A Key back up

// Press A down, and let up (same as two above)

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.

// Simulate (Ctrl + C) shortcut, which is copy for most applications

// This does the same as above

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

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.


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


About the Author

Brian Geiman
Software Developer GEA Refrigeration
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow do I stop the playback events by pressing a key? Pin
alexmbra12-Sep-10 11:17
memberalexmbra12-Sep-10 11:17 
GeneralFix for .NET 4 Pin
Thiru Thirunavukarasu25-Aug-10 15:04
memberThiru Thirunavukarasu25-Aug-10 15:04 
GeneralRe: Fix for .NET 4 Pin
Mr Pleco4-Sep-10 8:49
memberMr Pleco4-Sep-10 8:49 
GeneralMy vote of 5 Pin
Аslam Iqbal15-Jul-10 5:08
professionalАslam Iqbal15-Jul-10 5:08 
GeneralSuppressing Pin
Sen Dog31-May-10 3:09
memberSen Dog31-May-10 3:09 
QuestionIt doesn't seems to work in the 2010 beta ver ?? Pin
After-Darkness3-Apr-10 2:50
memberAfter-Darkness3-Apr-10 2:50 
Generale.Handled not working Pin
yannlh15-Mar-10 12:40
memberyannlh15-Mar-10 12:40 
QuestionKeyboard hook not working Pin
xpansive10-Mar-10 13:20
memberxpansive10-Mar-10 13:20 
GeneralTANX Pin
jerviz_764-Feb-10 6:27
memberjerviz_764-Feb-10 6:27 
GeneralIs there a way to import the MouseKeyboardLibrary.dll into the program itself?(As a class or something) Pin
kobyX221-Jan-10 10:25
memberkobyX221-Jan-10 10:25 
GeneralImported library, but can't use it [modified] Pin
paul_moore30-Nov-09 9:14
memberpaul_moore30-Nov-09 9:14 
GeneralRe: Imported library, but can't use it Pin
IAbstract16-Dec-09 19:30
memberIAbstract16-Dec-09 19:30 
Generalmouse_event/keybd_event = superseded Pin
Member 199257026-Nov-09 22:36
memberMember 199257026-Nov-09 22:36 
GeneralDouble Click [modified] Pin
mrboese18-Oct-09 0:47
membermrboese18-Oct-09 0:47 
GeneralRe: Double Click Pin
mehdi askeroglu18-Nov-09 14:54
membermehdi askeroglu18-Nov-09 14:54 
GeneralRe: Double Click Pin
mehdi askeroglu19-Nov-09 16:51
membermehdi askeroglu19-Nov-09 16:51 
QuestionShift sending Pin
eenow10-Sep-09 10:56
membereenow10-Sep-09 10:56 
GeneralMouseHook in Windows7 Pin
ascalonx1-Sep-09 5:20
memberascalonx1-Sep-09 5:20 
GeneralRight to left encoding Pin
hani142621-Aug-09 2:54
memberhani142621-Aug-09 2:54 
GeneralFeedback and a Suggestion Pin
Aidan Follestad12-Aug-09 10:33
memberAidan Follestad12-Aug-09 10:33 
GeneralI love you. <3 Pin
ahoffman6-Aug-09 16:20
memberahoffman6-Aug-09 16:20 
GeneralCongrats! Pin
jano_rajmond11-Apr-09 7:16
memberjano_rajmond11-Apr-09 7:16 
GeneralAmazing! Pin
Vadim Gavrilyuk9-Apr-09 9:10
memberVadim Gavrilyuk9-Apr-09 9:10 
QuestionColor Pipette ? Pin
Naked Coder31-Mar-09 6:57
memberNaked Coder31-Mar-09 6:57 
Generalrookie: can i use this DLL in a visual studio express 2008 c++ console app? [modified] Pin
4mla1fn4-Mar-09 9:09
member4mla1fn4-Mar-09 9:09 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 8 Aug 2008
Article Copyright 2008 by Brian Geiman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid