Click here to Skip to main content
15,887,135 members
Articles / Desktop Programming / MFC
Article

CKeystrokeEngine

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

Image 1

Introduction

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:

Hello CPians<ENTER><REPEAT 10>I love CP <END_REPEAT><ENTER>http://www.macroangel.com

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.

License

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


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

Comments and Discussions

 
GeneralRe: Superb! Pin
Mustafa Demirhan1-Mar-02 14:32
Mustafa Demirhan1-Mar-02 14:32 
GeneralRe: Superb! Pin
Philippe Lhoste18-Mar-02 0:25
Philippe Lhoste18-Mar-02 0:25 
GeneralRe: Superb! Pin
Mustafa Demirhan28-Mar-02 9:56
Mustafa Demirhan28-Mar-02 9:56 
GeneralExcellent! Pin
Philippe Lhoste27-Feb-02 0:16
Philippe Lhoste27-Feb-02 0:16 
GeneralRe: Excellent! Pin
Mustafa Demirhan28-Feb-02 21:10
Mustafa Demirhan28-Feb-02 21:10 
GeneralRe: Excellent! Pin
Nish Nishant28-Feb-02 21:40
sitebuilderNish Nishant28-Feb-02 21:40 
GeneralRe: Excellent! Pin
Mustafa Demirhan1-Mar-02 2:22
Mustafa Demirhan1-Mar-02 2:22 
GeneralRe: Excellent! Pin
Philippe Lhoste18-Mar-02 0:11
Philippe Lhoste18-Mar-02 0:11 
Thank you for your answer, I just saw it, CP didn't sent an e-mail to indicate my comment was answered Frown | :(

Keep the links, of course, it is informative and unobstrusive Wink | ;)

Mustafa Demirhan wrote:
Philippe Lhoste wrote: And I don't know (with the demo) how to send this to the Desktop

You cant do this with the demo program


I understand it is just a demo, we (the programmers) have to handle these problems... Although showing a way to do it would be cool Roll eyes | :rolleyes:

BTW, I made some more tests with your demo:
For <LWIN> and <RWIN> to be effective, the key must be sent to the Program Manager. I cannot use these keys to activate global shortcuts like <LWIN>R launching the Run dialog.
And still, sending global shortkeys to the Program Manager don't seems to activate Desktop icon shortcuts,

<APPS> is effective when sent to an application, it activates its context menu. If it is an editor, the context menu appears where the caret is.

Mustafa Demirhan wrote:
Philippe Lhoste wrote: Additional note: in your syntax, and in the demo, I see no way to quote < and > keys, ie. to send them instead of being interpreted.

If you just write that characters, the program will send it, as soon as you do not write a special character name, like <CTRL> or <ALT>. For example, if you write <Hello world>, the program sends <Hello World>.


Note: CodeProject ate your < and >... I restore them by hand, using &lt; and &gt;...

That's the problem: we cannot send a reserved sequence.

As I noted down before reading your answer:
"We can send < and > using them unquoted as long as they don't match a command of the engine. I still don't see how to send them when the sequence is seen as a command."

Perhaps you should define a special character or syntax to allow sending these characters literally, in all cases. Perhaps the classical backslash, but it is annoying because of its use in the paths. Unless it is interpreted literally if not followed by < and > or \. Or simply to double these characters: <<CTRL>> will send <CTRL> while <CTRL> will be interpreted by the engine.

I am not making these remarks just to lazily get improvements to your demo/engine, but also to help you improve your product, unless you already solved these issues there.

Thank you for this great engine! I will try and make it MFC independent, and perhaps more flexible on the syntax (before 2010, as my schedule goes Cry | :(( )

Regards.

--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
GeneralRe: Excellent! Pin
Mustafa Demirhan28-Mar-02 9:57
Mustafa Demirhan28-Mar-02 9:57 
GeneralExcuse my ignorance, but... Pin
Stan Shannon21-Feb-02 5:20
Stan Shannon21-Feb-02 5:20 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 5:58
Mustafa Demirhan21-Feb-02 5:58 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 6:07
Mustafa Demirhan21-Feb-02 6:07 
GeneralRe: Excuse my ignorance, but... Pin
Stan Shannon21-Feb-02 6:57
Stan Shannon21-Feb-02 6:57 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 6:59
Mustafa Demirhan21-Feb-02 6:59 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 6:55
Mustafa Demirhan21-Feb-02 6:55 
Questionhow to copy paste Pin
21-Feb-02 0:46
suss21-Feb-02 0:46 
AnswerRe: how to copy paste Pin
Mustafa Demirhan21-Feb-02 6:04
Mustafa Demirhan21-Feb-02 6:04 
GeneralOooh, LaTeX! Pin
Jörgen Sigvardsson20-Feb-02 10:51
Jörgen Sigvardsson20-Feb-02 10:51 
GeneralRe: Oooh, LaTeX! Pin
Mustafa Demirhan20-Feb-02 12:11
Mustafa Demirhan20-Feb-02 12:11 
GeneralRe: Oooh, LaTeX! Pin
João Filipe de Castro Ferreira1-Mar-02 1:06
João Filipe de Castro Ferreira1-Mar-02 1:06 
GeneralNice Work! Pin
Alexander Wiseman20-Feb-02 7:00
Alexander Wiseman20-Feb-02 7:00 
GeneralRe: Nice Work! Pin
Mustafa Demirhan20-Feb-02 12:04
Mustafa Demirhan20-Feb-02 12:04 
Generalniceeeeeeee Pin
Kashif Manzoor20-Feb-02 6:56
Kashif Manzoor20-Feb-02 6:56 
GeneralRe: niceeeeeeee Pin
Mustafa Demirhan20-Feb-02 12:04
Mustafa Demirhan20-Feb-02 12:04 
GeneralCool Pin
Matt Newman19-Feb-02 16:25
Matt Newman19-Feb-02 16:25 

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.