Click here to Skip to main content
15,395,698 members
Articles / Desktop Programming / MFC
Posted 18 Feb 2002


108 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.98/5 (29 votes)
21 Jun 2004CPOL4 min read
A set of classes used to send keystrokes to a selected window

Image 1


CKeystrokeEngine was originally written to allow users of my product, Macro Angel, to send keystrokes to the active 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 (which was also written for Macro Angel), it can send keystrokes to only a specified window.

A simple CKeystrokeEngine expression looks like this:


All special keys are put between '<' and '>' characters. For example to simulate an Enter key, you should use <ENTER>. Or to press 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 can also use <CTRL+ALT>, <CTRL+SHIFT> and <ALT+SHIFT>
  • To hold Shift key and many keys, use <SHIFT_LOCK>write your keys here<SHIFT_UNLOCK>
  • You can also 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, then the feature is disabled
void SetWndTitle (LPCSTR lpszTitle);
// if lpszClass == NULL, then 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)<br>
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)<br>
If bSendToWnd is true, then CKeystrokeEnginesends all keystrokes to a specified window. By this way, sending keystrokes to wrong windows is prevented.

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

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

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

SetExactMatch (bool bExact)<br>
Specified 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)<br>
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)<br>
If bReActivate is set to true, then CKeystrokeEngine automatically re-activates the window whenever it looses focus. If 0 seconds is entered, then the window is immediately activated when it looses 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 specified window looses focus, then SendKeys () function returns false.

bool SendKeys ()<br>
Finally, this functions sends the keys with the specifed options. Use this function setting 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 VBs SendKeys () syntax. Please don't want me to change the syntax. My customers are really happy with this syntax :)
  • This class is not used in the current version of Macro Angel. It will be used in the next version. Current version uses the same syntax, but the implementation is not as good as this. But this is class is not tested yet and may include some bugs. Please send me the bugs, so that I can fix them. If you find a bug, please send me the String that causes is not working properly. Also, please send me the real output and the expected output.
  • If you have any suggestions, please let me know.


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


About the Author

Mustafa Demirhan
Software Developer
United States United States
No Biography provided

Comments and Discussions

GeneralProblem Sending Keystroke to MDI application Pin
gewoog5-Oct-07 14:04
Membergewoog5-Oct-07 14:04 
Generalport to Pin
raventatatatata14-May-07 16:41
Memberraventatatatata14-May-07 16:41 
GeneralHotkeys Pin
Ole Martin Brynildsen16-Mar-07 2:52
MemberOle Martin Brynildsen16-Mar-07 2:52 
QuestionCan This Be Called From Vb6 Pin
killervolt12-Feb-07 20:12
Memberkillervolt12-Feb-07 20:12 
AnswerRe: Can This Be Called From Vb6 Pin
Mustafa Demirhan13-Feb-07 9:54
MemberMustafa Demirhan13-Feb-07 9:54 
QuestionRe: Can This Be Called From Vb6 Pin
raventatatatata20-May-07 16:02
Memberraventatatatata20-May-07 16:02 
General5/5 Pin
Rajesh R Subramanian9-Dec-05 19:21
professionalRajesh R Subramanian9-Dec-05 19:21 
GeneralRe: 5/5 Pin
Mustafa Demirhan9-Dec-05 23:57
MemberMustafa Demirhan9-Dec-05 23:57 
Questionhow to send to a process Pin
lbolanos27-Jan-05 10:31
Memberlbolanos27-Jan-05 10:31 
AnswerRe: how to send to a process Pin
Zhi Chen20-Nov-06 5:13
MemberZhi Chen20-Nov-06 5:13 
GeneralTerminal Services Pin
Anonymous17-Dec-04 11:42
MemberAnonymous17-Dec-04 11:42 
QuestionUpdated 22 Jun 2004? Pin
JL00225-Jun-04 3:48
MemberJL00225-Jun-04 3:48 
AnswerRe: Updated 22 Jun 2004? Pin
Mustafa Demirhan25-Jun-04 10:57
MemberMustafa Demirhan25-Jun-04 10:57 
Hi there,

The article was not actually updated. I put my e-mail adresses into all my articles and my stupidity paid of well by hundreds of spam e-mails every day.

So, I contacted to the CodeProject and told them that I want to remove my e-mail address. They did it in a few hours (thanks Wink | ;) ) but the articles showed as updated. I am sorry for the confusion.

Actually, I have another Keystroke Engine that is based on an MSDN articles code - this new engine works fine on Windows Xp/2000/NT but does not support win9x/me. It also works with different locales and it can send any unicode characaters. I think I will send another article with that engine Wink | ;)

PS: If you send me an e-mail via Codeproject's e-mail options, I can send it to you...

Mustafa Demirhan

"What we do in life echoes in eternity" - Gladiator
It's not that I'm lazy, it's just that I just don't care

QuestionIt is possible to send keystrokes without bringing the window forefront? Pin
JL0021-Jun-04 11:05
MemberJL0021-Jun-04 11:05 
AnswerRe: It is possible to send keystrokes without bringing the window forefront? Pin
Mustafa Demirhan2-Jun-04 7:32
MemberMustafa Demirhan2-Jun-04 7:32 
GeneralRe: It is possible to send keystrokes without bringing the window forefront? Pin
JL00214-Jun-04 8:33
MemberJL00214-Jun-04 8:33 
GeneralRe: It is possible to send keystrokes without bringing the window forefront? Pin
Mustafa Demirhan15-Jun-04 10:26
MemberMustafa Demirhan15-Jun-04 10:26 
GeneralRe: It is possible to send keystrokes without bringing the window forefront? Pin
sfirouza8-Dec-04 21:07
Membersfirouza8-Dec-04 21:07 
AnswerRe: It is possible to send keystrokes without bringing the window forefront? Pin
InfiniTeEn10-Jul-04 4:36
MemberInfiniTeEn10-Jul-04 4:36 
General&lt;Repeat 10&gt; Great! &lt;end repeat&gt; Pin
BlackDice20-May-04 4:52
MemberBlackDice20-May-04 4:52 
GeneralRe: &lt;Repeat 10&gt; Great! &lt;end repeat&gt; Pin
Mustafa Demirhan20-May-04 5:26
MemberMustafa Demirhan20-May-04 5:26 
QuestionCan not Compile In VC.NET2003, Miss Afximpl.h Pin
zhbjhwli13-Mar-04 15:38
Memberzhbjhwli13-Mar-04 15:38 
AnswerRe: Can not Compile In VC.NET2003, Miss Afximpl.h Pin
Mustafa Demirhan13-Mar-04 23:05
MemberMustafa Demirhan13-Mar-04 23:05 
GeneralImplementation part of that version of MFC used in this applicaiton Pin
Herbert Yu17-Jan-05 10:24
MemberHerbert Yu17-Jan-05 10:24 
GeneralProblem with WinXP Pin
dressman198120-Jan-04 8:18
Memberdressman198120-Jan-04 8: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.