Click here to Skip to main content
15,885,109 members
Articles / Desktop Programming / Win32

Mouse Emulating Software

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

You can find the Russian version of the article here:

HowToDoIt

Image 1

Introduction

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.

Background

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:

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

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

C++
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:

C++
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;
         }
     }
 
     if(bEmulEn)
     {
         switch ( hookStruct->vkCode )
         {
              case KeyClickLeft:
              {
                // emulating left mouse button click
              }
              break;
              case KeyClickRight:
              {
                // emulating right mouse button click
              }
              break;
              case KeyMoveLeft:
              {
                // move cursor left
              }
              break;
              case KeyMoveUP:
              {
                 // move cursor up
              }
              break;
              case KeyMoveRight:
              {                
                // move cursor right
              }
              break;
              case KeyMoveDown:
              {
                 // move cursor down
              }
              break;
         }
    }
}

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

C++
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:

C++
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).

Tips

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.

References

  1. http://msdn.microsoft.com/en-us/library/ms644959(v=vs.85).aspx
  2. http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx

History

  • 10th May, 2011: Initial version

License

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