Click here to Skip to main content
15,663,872 members
Articles / Desktop Programming / Win32
Posted 10 May 2011

Tagged as


54 bookmarked

Mouse Emulating Software

Rate me:
Please Sign up or sign in to vote.
4.91/5 (14 votes)
19 Oct 2012CPOL3 min read
A simple software based mouse emulator

You can find the Russian version of the article here:


Image 1


This project demonstrates the use of a programming hook technique to emulate mouse by means of the keyboard. The mouse emulator software is useful in cases when the “real” mouse is broken (or absent) and you need to perform some operations which require the mouse pointing device.


One day while working on an old computer, I ran into a problem... the USB port was broken and the mouse PS/2 was absent. I decided to write a simple program which could take the place of the mouse device.

Using the Code

The idea of “virtual mouse” software is the following. First of all, it is necessary to choose and assign keys for the corresponding mouse actions. The chosen assignment is as follows:

  • move pointer left (NUM4)
  • move pointer right (NUM6)
  • move pointer up (NUM8)
  • move pointer down (NUM5)
  • left mouse button click (NUM7)
  • right mouse button click (NUM9)

The source code for these assignments looks as follows:

// keys to control cursor
#define KeyMoveLeft                VK_NUMPAD4
#define KeyMoveRight               VK_NUMPAD6
#define KeyMoveUP                  VK_NUMPAD8
#define KeyMoveDown                VK_NUMPAD5
// keys to emulate mouse buttons
#define KeyClickLeft               VK_NUMPAD7
#define KeyClickRight              VK_NUMPAD9

One more necessary thing is to assign a button (it may be a combination of keys) which would control the state of the “virtual mouse”. It could be switched on and off, if necessary. For this purpose, the following definition is present in the code:

// key to switch on/off MouseEmulate software
#define KeyMouseEmulateEnDis       VK_F7

The next obvious actions are:

  • creation of the function for keys processing
  • starting keyboard hook from the “main” function

What is a keyboard hook? A hook is a mechanism by which an application can intercept events, such as messages, mouse actions, and keystrokes. A function that intercepts a particular type of event is known as a hook procedure. A hook procedure can act on each event it receives, and then modify or discard the event [1]. To start a keyboard hook, it is necessary to write the callback function which will be handled on key pressed. The callback function is:

LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam)
     if (nCode == HC_ACTION)
         if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN || 
             wParam == WM_SYSKEYUP || wParam == WM_KEYUP)
            CheckKey (nCode, wParam, lParam);
     return CallNextHookEx(hHook, nCode, wParam, lParam);

Here, the CheckKey(…) function it is exactly a “key processor”. The content (skeleton) of this function is presented below:

void CheckKey( int nCode, WPARAM wParam, LPARAM lParam )
     // switch on/off the MouseEmulate program
     if (wParam == WM_SYSKEYUP || wParam == WM_KEYUP)
         if( hookStruct->vkCode == KeyMouseEmulateEnDis )
            bEmulEn = bEmulEn ? false: true;
         switch ( hookStruct->vkCode )
              case KeyClickLeft:
                // emulating left mouse button click
              case KeyClickRight:
                // emulating right mouse button click
              case KeyMoveLeft:
                // move cursor left
              case KeyMoveUP:
                 // move cursor up
              case KeyMoveRight:
                // move cursor right
              case KeyMoveDown:
                 // move cursor down

It is possible to start the hook from the “main” function:

int APIENTRY WinMain( HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
     hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHook, hInstance , 0);
     while (GetMessage(NULL,NULL,0,0)) ; // NOP while not WM_QUIT
     return UnhookWindowsHookEx(hHook);

More details of the function SetWindowsHookEx(…) are available in [2].

It is preferable to use the technique of determining the step for moving the cursor depending on whether the button (which is responsible for moving the cursor) is long-pressed or not (short-pressed). The following function is responsible for that idea:

void StepCalculate()
     unsigned short speedMax = 40;
     unsigned short dt = tNow-tBefore;
     float dtMax = 30.0f;
     step = (int)(speedMax * dtMax/(tNow-tBefore));

Here, the speedMax variable is the biggest step (40 pixels) when moving the cursor by means of a key long-pressed action. The dt value characterizes the time difference between two identical key pressing (long-pressed key is considered as multiple key pressing).


To run this software each time Windows starts up, just place the “.exe” file into the “startup” directory.

When using this software in text redactors, push the CTRL button to avoid the typing of unexpected char values.

Points of Interest

The first time I wrote this software, the left mouse button click was emulated as a fast click combination of the “move cursor left”-“move cursor right” keys. The right button mouse click was emulated similarly, as a “move cursor right” -“move cursor left” keys click combination. It was very suitable but some bugs remained. It would be nice to realize (or recover) such an idea in the future.




  • 10th May, 2011: Initial version


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

Written By
Software Developer Samsung SURC
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionit doesn't work on win7 Pin
pj220_200625-Mar-12 21:19
pj220_200625-Mar-12 21:19 
AnswerRe: it doesn't work on win7 Pin
Viktor Signaievskyi7-Apr-12 11:40
Viktor Signaievskyi7-Apr-12 11:40 
Ohh, so pitty! Dude, I haven't tested it nor "Win7" nor "Red Hat";) But, you can analyze hooking technique for the OS you interested in. As I know Win7 has a lot of limitations in rights and access. Once, I wasn't able to create the file in a system directory. So, you has to dig in that direction and good luck!)
GeneralMouse Keys Pin
Indivara10-May-11 20:11
professionalIndivara10-May-11 20:11 
GeneralKeyboardHook tutorial Pin
QuiOui10-May-11 17:36
QuiOui10-May-11 17:36 
GeneralRe: KeyboardHook tutorial Pin
Viktor Signaievskyi11-May-11 7:55
Viktor Signaievskyi11-May-11 7:55 

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.