CKeystrokeEngine'�Sending Keystrokes to a Selected Window


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


CKeystrokeEngine was originally written to allow users of my product, Macro Angel, to send keystrokes to the active window or to a selected window. Its functionality is similar to VB's SendKeys's functionality, but it allows more complex operations. In addition to sending keystrokes to the active window, by the help of CWindowEngine, it can send keystrokes to only a specified window.

A simple CKeystrokeEngine expression looks like this:

Hello World!<ENTER><REPEAT 10>I love open source<ENTER><END_REPEAT>
            Mustafa Demirhan

All special keys are put between '<' and '>' characters. For example, to simulate an Enter key, you should use <ENTER>. Or, to press the Shift key, use <SHIFT>. Here are some more examples:

  • To send Ctrl+A, use <CTRL>A.
  • To send Alt+F4, use <ALT><F4>.
  • To send Ctrl+Alt+Shift+A, use <CTRL+ALT+SHIFT>A.
  • You also can use <CTRL+ALT>, <CTRL+SHIFT>, and <ALT+SHIFT>.
  • To hold the Shift key and many other keys, use <SHIFT_LOCK>write your keys here<SHIFT_UNLOCK>.
  • You also can use <ALT_LOCK>, <ALT_UNLOCK>, <CTRL_LOCK>, and <CTRL_UNLOCK>.
  • To add a delay, use <DELAY xxx> where xxx is the amount of time in miliseconds. For example, <DELAY 1500> waits 1.5 seconds.
  • To repeat a sequence of keys 100 times, use <REPEAT 100>you keys here<END_REPEAT>. Please note that nested repeats are not supported.

All other keys are listed in the sample application. Here are the public functions of CKeystrokeEngine:

CKeystrokeEngine (const CString &sKeys);

void SetPause (bool bPause, int nPause);
void SetSendToWnd (bool bSendToWnd);
// if lpszTitle == NULL, the feature is disabled
void SetWndTitle (LPCSTR lpszTitle);
// if lpszClass == NULL, the feature is disabled
void SetWndClass (LPCSTR lpszClass);
void SetExactMatch (bool bExact);
void SetCaseSensitive (bool bCaseSensitive);
void SetReActivate (bool bReactivate, int nMiliseconds);

// Tadaaa. Here is the best function...
bool SendKeys ();
SetPause (bool bPause, int nPause)
Adjusts how much time CKeystrokeEngine should wait in between each key press when sending keystrokes. On some faster machines, CKeystrokeEngine may type the keys too quickly, flooding the keyboard buffer and causing unpredictable results. Increasing this setting will prevent this from occurring.

SetSendToWnd (bool bSendToWnd)
If bSendToWnd is true, CKeystrokeEnginesends all keystrokes to a specified window. By doing this, sending keystrokes to wrong windows is prevented.

If bSendToWnd is set to false, the active window gets all of the keystrokes.

SetWndTitle (LPCSTR lpszTitle)
Sets the window title to which the keystrokes will be sent. If lpszTitle is set to NULL, CKeystrokeEngine does not searh for a window title. Note that this is effective only if SetSendToWnd (true) is used.

SetWndClass (LPCSTR lpszClass)
Sets the window class to which the keystrokes will be sent. If lpszClass is set to NULL, CKeystrokeEngine does not search for a window class. Note that this is effective only if SetSendToWnd (true) is used.

SetExactMatch (bool bExact)
Specifies whether the Windows title entered is a partial title match or must match exactly. When set to false, CKeystrokeEngine will search for window titles that contain that text. Note that this is effective only if SetSendToWnd (true) is used.

SetCaseSensitive (bool bCaseSensitive)
If enabled, Window Title/Window Class and Text searches would be case sensitive. Note that this is effective only if SetSendToWnd (true) is used.

SetReActivate (bool bReActivate, int nMiliseconds)
If bReActivate is set to true, CKeystrokeEngine automatically re-activates the window whenever it loses focus. If 0 seconds is entered, the window is immediately activated when it loses focus. Otherwise, CKeystrokeEngine waits for the specified amount of time and then re-activates the window.

Adding a delay before re-activating windows prevents collisions between applications that are trying to send keystrokes at the same time.

Note that this is effective only if SetSendToWnd (true)is used. Also, if bReActivate is set to false and the specified window loses focus, the SendKeys () function returns false.

bool SendKeys ()
Finally, this function sends the keys with the specifed options. Use this function to set all attributes of CSendKeystrokes.

To get more information about the supported keywords, please use the sample application...

Some Points That You Should Keep in Mind

  • To use CKeystrokeEngine in your applications, copy KeystrokeEngine.cpp, KeystrokeEngine.h, WindowEngine.cpp, and WindowEngine.h into your project and include KeystrokeEngine.h in your cpp file.
  • This class uses its own syntax instead of using VB's SendKeys () syntax.
  • If you have any suggestions, please let me know. My e-mail address is emindemirhan@yahoo.com.


Download demo project - 103 Kb
Download demo executable - 35 Kb
Download source - 10 Kb


  • Probably you forgot an easy function...

    Posted by laisan86 on 06/10/2004 09:04am

    Do you think it is possible for let users send the key stroke's expression to a handle of window (HWND) directly? Sometime, we will get the HWND of child-windows (like controls on dialog-boxes or forms) when we start up our applications, and we will need them after that.

  • Thanks a lot

    Posted by Legacy on 02/18/2004 08:00am

    Originally posted by: stevechen

    i am looking for a key map class, and found your keyengine is the best one, thanks a lot.

  • Possible with vb.Net?

    Posted by Legacy on 11/06/2003 08:00am

    Originally posted by: onur orek

    Is it possible to do the same thing using vb.Net (which MFC libraries one should include?)
    onur orek

  • mine is not working

    Posted by Legacy on 08/14/2003 07:00am

    Originally posted by: Guilherme

    I'm new in MFC. Could a good guy send me a source of a simple program to send keys to notepad?

    Guilherme - Brazil

  • Weird Bug

    Posted by Legacy on 08/04/2003 07:00am

    Originally posted by: Brandon

    For some reason its activating Microsoft Excel? This is weird.

    Let me further explain... I was testing its ability to send keystrokes to a DirectX window so I used EverQuest IM aka EQIM(the first DX programing I found on my computer). I sent a simple "test" not even using an <ENTER> and it was activating microsoft excel.

    Windows 2000 Professional OS

  • Doesn't work under WinXP

    Posted by Legacy on 07/07/2003 07:00am

    Originally posted by: Licu Pliciu

    It works fine under Win98 SE but under XP it just focuses the target window and then nothing.

  • No Title

    Posted by Legacy on 07/05/2003 07:00am

    Originally posted by: Muhammad Zahid

    Mustafa's Article and software demo is extremely useful for those who are learning MFC. This demo is to the point and teaches exactly how a new programmer can capture keyboard commands into his program. Most of the books don't touch this area this way and students remain unsure of how to communicate with the window they create in Visual C++.
    Great Work by a knowledgable person.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date