Click here to Skip to main content
12,248,742 members (37,925 online)
Click here to Skip to main content
Add your own
alternative version


138 bookmarked

InputManager library - Track user input and simulate input (mouse and keyboard)

, 13 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A library that contains classes to simulate user input and track user input (mouse and keyboard), in C# and VB.NET.


The InputManager library allows you to take control over the whole user common input. From tracking (hooking) user input to simulating user input. It allows you to send and receive global mouse and keyboard messages, in one easy to use library. It supports DirectX input simulation, which means you can send mouse or keyboard messages to any DirectX game you want, and the game will register it correctly.

With this library, you can create a complete macro recording application; it's well documented, and can be easily used with any application.


Starting my way to create my own Macro Recording application, today known as Mouse Recorder Pro, I needed to find a way to register mouse and keyboard messages and track them down. After a number of version updates for Mouse Recorder Pro, and after improving my own coding skills, I made this library to help others.

The hook classes in this library were not written by me (but they were modified by me) to be honest. I don't remember the person who helped with it, but if I am able to track him, I will update this article.

The other classes were written by me, with some research and a lot of reading. The main problem I had was to be able to register user input in DirectX games, which I managed to accomplish afterwards.

The Concept (Or, How it Works)

The library was written in VB.NET, and tested on many systems (as part of its use in the Mouse Recorder Pro commercial application).

The keyboard and mouse messages are being sent with the SendInput API, which is a bit more flexible than other APIs. With the SendInput API, your application can send mouse and keyboard messages in a low level (as DirectX is). This allows you to register those messages in any DirectX game.

The mouse and keyboard hook classes allow you to track the user input. The classes use the SetWindowsHookEx API. This API "binds" any new mouse or keyboard message to a specific method in the hook classes. Every time a new message arrives, Windows calls that method. Within that method, an event is raised to inform you that a new input message has arrived.


Mouse Recorder Pro 2 - it uses the InputManager library!

Using the code

To use this library, we first need to add a reference for it in our project; download the InputManager library from the link above, and extract it. Now do the following:

  1. Open up your project.
  2. Right click on "References" and "Add Reference".
  3. Select the "Browse" tab and select the extracted "InputManager.dll".
  4. Accept all dialogs.
  5. To make this project work in Debug mode, double click on Properties, go to "Debug", and uncheck "Enable the Visual Studio hosting process".

I wasn't able to use the class hook classes (used for tracking) with .NET Framework 4, so if you are using .NET Framework 4, please change the target framework to 3.5 or any framework below by going to the project properties and selecting a different version of the .NET Framework.

Now, let's add the "using" statement to make it a bit easier for coding. Add the following using statement:

using InputManager;

OK, let's get to know how to use this library.

Tracking User Mouse and Keyboard Messages


Download this hook example at the top

To track the user mouse and keyboard messages, we need to set up a hook. How do we do that? With InputManager, it's pretty easy.

To track user keyboard messages, in your Form_Load event handling method, add the following code:

//Adding keyboard event handlers and installing the hook
KeyboardHook.KeyDown += new KeyboardHook.KeyDownEventHandler(KeyboardHook_KeyDown);
KeyboardHook.KeyUp += new KeyboardHook.KeyUpEventHandler(KeyboardHook_KeyUp);

The first two lines allow us to track the keyboard messages. This is done by two methods we will create to handle those events. The third line tells the InputManager library to set up a hook and start reading the keyboard messages.

Now let's add the two methods we talked about earlier:

void KeyboardHook_KeyUp(int vkCode)
    //Everytime the users releases a certain key up,
    //your application will go to this line
    //Use the vKCode argument to determine which key has been released

void KeyboardHook_KeyDown(int vkCode)
    //Everytime the users holds a certain key down,
    //your application will go to this line
    //Use the vKCode argument to determine which key is held down

To convert those keys to a string instead of a virtual key code, use this simple technique:

string keyString = ((Keys)vkCode).ToString();
//Using the Keys Enum ToString method for translating

To track user mouse messages, in your Form_Load event handling method, add the following code:

//Adding mouse event handlers and installing the hook
MouseHook.MouseEvent += new MouseHook.MouseEventEventHandler(MouseHook_MouseEvent);
MouseHook.MouseMove += new MouseHook.MouseMoveEventHandler(MouseHook_MouseMove);
MouseHook.WheelEvent += new MouseHook.WheelEventEventHandler(MouseHook_WheelEvent);

The first three lines tell the InputManager library to add event handlers for each mouse message type (mouse button events, mouse move event, and wheel scrolled event). We will add the methods that handle those messages below. The fourth line tells the InputManager class to install the hook and start receiving the messages.

Let's add the methods that handle those messages:

void MouseHook_WheelEvent(MouseHook.MouseWheelEvents wEvent)
    //Event time the wheel was scrolled the application will go to this line
    //Using the wEvent argument we can detect 
    //if the mouse was scrolled forward or backward

void MouseHook_MouseMove(MouseHook.POINT ptLocat)
    //Everytime the mouse moved, the application will go to this line
    //Using the ptLocat arguments you can detect 
    //which point the mouse cursor moved to

void MouseHook_MouseEvent(MouseHook.MouseEvents mEvent)
    //Everytime a mouse button changed it's state 
    //(from up to down or down to up) the application will go to this line
    //Using the mEvent argument we can detect which button changed it's state

To detect if the wheel was scrolled up or down within the MouseHook_WheelEvent method, we use the MouseHook.MouseWheelEvents enum:

string scrollDirection;
if (wEvent == MouseHook.MouseWheelEvents.ScrollDown)
    scrollDirection = "scrolled down";
else scrollDirection = "scrolled up";

To detect the position (X and Y) of the mouse every time it moves, we use the ptLocat argument within the MouseHook_MouseMove method:

textBox1.Text = "The mouse cursor moved to: " + ptLocat.x + ", " + ptLocat.y;
//Updating a textbox text to the current mouse position

To detect which button changed its state, we use the the mEvent argument within the MouseHook_MouseEvent method:

textBox1.Text = "Mouse button changed state to: " + mEvent.ToString();
//Updates the textbox to show which button changed it's state last time

You can use the switch statement to handle each mouse button/state change differently:

switch (mEvent)
   case MouseHook.MouseEvents.LeftDown:
       //Left button is down, do something here

   case MouseHook.MouseEvents.LeftUp:
        //Left button is up, do something here

Sending Mouse and Keyboard Messages (With DirectX Support)

To send keyboard messages (key strokes), we use the Keyboard class in the InputManager library.

To send key down/key up messages, we use the Keyboard.KeyDown or Keyboard.KeyUp methods. An example of writing "hello" in a textbox:

ExampleText.Focus(); //Sets focus on the example text

//Writing Hello with keys sending





To send shortcut keys, we use the Keyboard.ShortcutKeys method, with an array of Keys:

ExampleText.Focus(); //Sets focus on the example textbox

//Sends Shift + Control + Left to select a word
Keys[] keys = new Keys[] { Keys.RShiftKey, Keys.RControlKey, Keys.Left };

To send mouse messages (mouse key strokes, mouse movements, wheel scroll), we use the Mouse class in the InputManager library.

To move the mouse to a certain location (in screen coordinates), we use the Mouse.Move method:

Mouse.Move(Point.X, Point.Y);
//Moves the mouse to a certain location on the screen

To perform a mouse click message, we use the Mouse.PressButton method:

Mouse.PressButton(Mouse.MouseKeys.Left); //Performing a left click

To send mouse button down or up messages, we use the Mouse.ButtonDown/Mouse.ButtonUp methods:

Mouse.ButtonDown(Mouse.MouseKeys.Left); //Holding the left mouse down
Mouse.ButtonUp(Mouse.MouseKeys.Left); //Releasing the left mouse button

To scroll up or down, we use the Mouse.Scroll method:

Mouse.Scroll(Mouse.ScrollDirection.Down); //Scroling down
Mouse.Scroll(Mouse.ScrollDirection.Up); //Scrolling up


Download this input simulating example at the top

Points of Interest

Writing this library (parts of it) was really fun and a bit challenging. There are other ways of sending mouse and keyboard messages with different APIs which don't support DirectX and are easier to use, but I wanted to simplify it and create my own library, so you don't even need to know what API means. With this library, you can send and receive input messages easily!


  • 13.10.2010: Added an explanation on how the library works (the concept behind it).


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


About the Author

Software Developer Nemex Studios
Israel Israel
My name is Shay and I'm 21 years old.
At age 16 I created Nemex Studios (, an application development "company" (not official).
I'm the developer of "Mouse Recorder Pro", "Mouse Shaker" and many other applications, developed to help the community.

You may also be interested in...

Comments and Discussions

QuestionNot working for function keys Pin
Member 1201008127-Oct-15 16:25
memberMember 1201008127-Oct-15 16:25 
QuestionSending and catching mouse position input as a low level (Hardware/Direct) Input Pin
Member 1158266823-Oct-15 9:25
memberMember 1158266823-Oct-15 9:25 
QuestionCan i have the source code for saving the hook and playing back ? Pin
ImmadiDeepthi27-Sep-15 16:50
memberImmadiDeepthi27-Sep-15 16:50 
AnswerRe: Can i have the source code for saving the hook and playing back ? Pin
shynet14-Oct-15 22:14
membershynet14-Oct-15 22:14 
QuestionBroken? Pin
Member 1042119730-Jul-15 12:38
memberMember 1042119730-Jul-15 12:38 
AnswerRe: Broken? Pin
Member 1042119731-Jul-15 1:09
memberMember 1042119731-Jul-15 1:09 
NewsRe: Broken? Pin
Member 1042119731-Jul-15 1:15
memberMember 1042119731-Jul-15 1:15 
QuestionDownload not work? Pin
Member 1185017920-Jul-15 10:19
memberMember 1185017920-Jul-15 10:19 
AnswerRe: Download not work? Pin
shynet21-Jul-15 11:38
membershynet21-Jul-15 11:38 
QuestionNot working reliably - any ideas? Pin
Magnamus8-Jul-15 8:16
memberMagnamus8-Jul-15 8:16 
QuestionIt doesn't work for a specific DirectX game. Pin
Tribute2PanterA28-Jun-15 0:44
memberTribute2PanterA28-Jun-15 0:44 
GeneralIt sounds so promising. Pin
TributeToPantera22-Jun-15 17:33
memberTributeToPantera22-Jun-15 17:33 
Generalapp to record Test case and record mouse movement and keyboard clicks Pin
Member 117448536-Jun-15 21:31
memberMember 117448536-Jun-15 21:31 
QuestionMouse input / output in pulses? Pin
Member 1166514820-May-15 2:25
memberMember 1166514820-May-15 2:25 
QuestionYou saved my day with this library Pin
niekvv22-Apr-15 9:13
memberniekvv22-Apr-15 9:13 
QuestionHandle Pin
Member 1152076423-Mar-15 4:54
memberMember 1152076423-Mar-15 4:54 
Questionuser custom keyboard.keydown Pin
Member 107038664-Mar-15 4:10
memberMember 107038664-Mar-15 4:10 
AnswerRe: user custom keyboard.keydown Pin
Member 1042119730-Jul-15 12:51
memberMember 1042119730-Jul-15 12:51 
SuggestionMessage Automatically Removed Pin
31-Aug-14 1:33
memberMember 1104824331-Aug-14 1:33 
QuestionKeyStrokes ans Mouseclicks send to background Process Pin
Member 109940195-Aug-14 15:20
memberMember 109940195-Aug-14 15:20 
AnswerRe: KeyStrokes ans Mouseclicks send to background Process Pin
shynet7-Aug-14 10:20
membershynet7-Aug-14 10:20 
QuestionDoesn't seem to install hooks correctly in WPF? Can't think of any reason it wouldn't work. Pin
aitee.meh15-Jun-14 22:51
memberaitee.meh15-Jun-14 22:51 
AnswerRe: Doesn't seem to install hooks correctly in WPF? Can't think of any reason it wouldn't work. Pin
aitee.meh16-Jun-14 15:12
memberaitee.meh16-Jun-14 15:12 
GeneralRe: Doesn't seem to install hooks correctly in WPF? Can't think of any reason it wouldn't work. Pin
shynet19-Jun-14 9:39
membershynet19-Jun-14 9:39 
QuestionRepeating Problem Pin
Member 1068860121-Mar-14 14:21
memberMember 1068860121-Mar-14 14:21 
AnswerRe: Repeating Problem Pin
shynet22-Mar-14 10:11
membershynet22-Mar-14 10:11 
GeneralRe: Repeating Problem Pin
Member 1068860122-Mar-14 23:59
memberMember 1068860122-Mar-14 23:59 
QuestionKeyDown/KeyUp doesn't repeat character in text fields Pin
elsporko20-Feb-14 8:44
memberelsporko20-Feb-14 8:44 
AnswerRe: KeyDown/KeyUp doesn't repeat character in text fields Pin
shynet21-Feb-14 1:55
membershynet21-Feb-14 1:55 
GeneralRe: KeyDown/KeyUp doesn't repeat character in text fields Pin
elsporko1-Mar-14 13:49
memberelsporko1-Mar-14 13:49 
GeneralRe: KeyDown/KeyUp doesn't repeat character in text fields Pin
shynet8-Mar-14 11:27
membershynet8-Mar-14 11:27 
QuestionTracking Specific Mouse Pin
Vet Ralph4-Feb-14 3:53
memberVet Ralph4-Feb-14 3:53 
AnswerRe: Tracking Specific Mouse Pin
shynet6-Feb-14 22:29
membershynet6-Feb-14 22:29 
SuggestionUpdated the class and it is working with .NET 4.5.1 and Windows 8.1! Pin
Jovan Nikolov30-Jan-14 12:28
memberJovan Nikolov30-Jan-14 12:28 
GeneralRe: Updated the class and it is working with .NET 4.5.1 and Windows 8.1! Pin
shynet6-Feb-14 22:30
membershynet6-Feb-14 22:30 
QuestionKeyboard symbol generator Pin
Member 102807559-Jan-14 4:59
memberMember 102807559-Jan-14 4:59 
AnswerRe: Keyboard symbol generator Pin
shynet10-Jan-14 0:52
membershynet10-Jan-14 0:52 
QuestionSimulate Alt+ tab or Ctrl+Alt+tab Pin
chandan028514-Sep-13 21:45
memberchandan028514-Sep-13 21:45 
AnswerRe: Simulate Alt+ tab or Ctrl+Alt+tab Pin
shynet19-Sep-13 5:16
membershynet19-Sep-13 5:16 
QuestionHow can I prevent an input event from going out to any other process? Pin
jol33328-Apr-13 0:57
memberjol33328-Apr-13 0:57 
AnswerRe: How can I prevent an input event from going out to any other process? Pin
shynet2-May-13 6:45
membershynet2-May-13 6:45 
GeneralMy vote of 5 Pin
Member 997387814-Apr-13 13:29
memberMember 997387814-Apr-13 13:29 
QuestionQuestion Pin
patrick cavalcante4-Mar-13 11:19
memberpatrick cavalcante4-Mar-13 11:19 
AnswerRe: Question Pin
shynet8-Mar-13 1:26
membershynet8-Mar-13 1:26 
QuestionAdd to a Project Pin
Rafo21059-Jan-13 21:02
memberRafo21059-Jan-13 21:02 
Hi Shay, first I want to say that this code is awesome, but there is one question I'd like to ask:

I've dowloaded the source code just because I don't want to have a separated DLL with my program because it's small, but when I add the InputManager.vb file to my project and run it, the scroll command won't work and also it won't register the events (like mouse click, etc.)

My project is just a form with a textbox and a listbox full of crap, to test the scroll. When the dll is added as a reference everything works wonderfully, but the problem begins when I add the .vb file directly to my project.

Please, answer me as soon as posible. Thanks again for this awesome code.

modified 10-Jan-13 12:41pm.

AnswerRe: Add to a Project Pin
shynet10-Jan-13 7:30
membershynet10-Jan-13 7:30 
QuestionIt WOrked :D Pin
Deny Ccit2-Jan-13 5:04
memberDeny Ccit2-Jan-13 5:04 
GeneralAwesome Pin
wtfblubb31-Oct-12 5:19
memberwtfblubb31-Oct-12 5:19 
GeneralRe: Awesome Pin
shynet7-Nov-12 9:44
membershynet7-Nov-12 9:44 
GeneralRe: Awesome Pin
shynet30-Nov-12 4:18
membershynet30-Nov-12 4:18 

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.160426.1 | Last Updated 13 Oct 2010
Article Copyright 2010 by shynet
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid