Click here to Skip to main content
11,716,249 members (85,745 online)
Click here to Skip to main content

CKeystrokeEngine

, 21 Jun 2004 CPOL 245.3K 7.1K 107
Rate this:
Please Sign up or sign in to vote.
A set of classes used to send keystrokes to a selected window

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)
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, 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)
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)
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)
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)
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, 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 ()
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 Smile | :)
  • 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)

Share

About the Author

Mustafa Demirhan
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Problem with WinXP Pin
Mustafa Demirhan20-Jan-04 9:50
memberMustafa Demirhan20-Jan-04 9:50 
GeneralRe: Problem with WinXP Pin
JessicaLampe20-Jan-04 19:25
sussJessicaLampe20-Jan-04 19:25 
GeneralRe: Problem with WinXP Pin
Mustafa Demirhan20-Jan-04 22:19
memberMustafa Demirhan20-Jan-04 22:19 
GeneralRe: Problem with WinXP Pin
JessicaLampe20-Jan-04 23:28
sussJessicaLampe20-Jan-04 23:28 
GeneralRe: Problem with WinXP Pin
Mustafa Demirhan21-Jan-04 8:52
memberMustafa Demirhan21-Jan-04 8:52 
GeneralRe: Problem with WinXP Pin
JessicaLampe21-Jan-04 9:40
sussJessicaLampe21-Jan-04 9:40 
GeneralRe: Problem with WinXP Pin
JessicaLampe21-Jan-04 9:40
sussJessicaLampe21-Jan-04 9:40 
GeneralRe: Problem with WinXP Pin
JessicaLampe22-Jan-04 2:30
sussJessicaLampe22-Jan-04 2:30 
GeneralBug: SendKeys produces memory leaks Pin
ThoRa15-May-03 21:57
memberThoRa15-May-03 21:57 
GeneralRe: Bug: SendKeys produces memory leaks Pin
Mustafa Demirhan15-May-03 22:08
memberMustafa Demirhan15-May-03 22:08 
GeneralRe: Bug: SendKeys produces memory leaks Pin
ThoRa18-May-03 20:28
memberThoRa18-May-03 20:28 
GeneralSome suggestion Pin
Davide Zaccanti13-Apr-03 21:03
memberDavide Zaccanti13-Apr-03 21:03 
Generalfirst thanks - then starter code simply to press key when invoked Pin
Michael Andras4-Apr-03 3:26
sussMichael Andras4-Apr-03 3:26 
GeneralRe: first thanks - then starter code simply to press key when invoked Pin
Mustafa Demirhan4-Apr-03 8:31
memberMustafa Demirhan4-Apr-03 8:31 
GeneralError Compiling my own project. Pin
Ash Ketchum24-Feb-03 14:01
memberAsh Ketchum24-Feb-03 14:01 
GeneralRe: Error Compiling my own project. Pin
Mustafa Demirhan4-Mar-03 14:14
memberMustafa Demirhan4-Mar-03 14:14 
GeneralRe: Error Compiling my own project. Pin
John Display10-Apr-03 9:29
memberJohn Display10-Apr-03 9:29 
GeneralRe: Error Compiling my own project. Pin
Mustafa Demirhan10-Apr-03 12:27
memberMustafa Demirhan10-Apr-03 12:27 
GeneralRe: Error Compiling my own project. Pin
John Display10-Apr-03 22:17
memberJohn Display10-Apr-03 22:17 
GeneralBest i've ever found! Pin
haiyang20-Dec-02 16:50
memberhaiyang20-Dec-02 16:50 
GeneralRe: Best i've ever found! Pin
Mustafa Demirhan20-Dec-02 19:20
memberMustafa Demirhan20-Dec-02 19:20 
Generalvery good Pin
yary19-Dec-02 2:08
memberyary19-Dec-02 2:08 
GeneralRe: very good Pin
Mustafa Demirhan19-Dec-02 11:37
memberMustafa Demirhan19-Dec-02 11:37 
GeneralIt won't work for multi-window automation Pin
stain15-Apr-02 22:49
memberstain15-Apr-02 22:49 
GeneralRe: It won't work for multi-window automation Pin
Mustafa Demirhan16-Apr-02 7:16
memberMustafa Demirhan16-Apr-02 7:16 
GeneralFixes for VS .NET Pin
Marcello14-Mar-02 6:31
memberMarcello14-Mar-02 6:31 
GeneralRe: Fixes for VS .NET Pin
Mustafa Demirhan14-Mar-02 10:32
memberMustafa Demirhan14-Mar-02 10:32 
GeneralKeystroke for Delete button Pin
harirk3-Mar-02 22:01
memberharirk3-Mar-02 22:01 
GeneralDont seem to work everytime Pin
Magnus H3-Mar-02 21:29
memberMagnus H3-Mar-02 21:29 
GeneralRe: Dont seem to work everytime Pin
Mustafa Demirhan3-Mar-02 21:56
memberMustafa Demirhan3-Mar-02 21:56 
GeneralRe: Dont seem to work everytime Pin
Magnus H3-Mar-02 21:55
memberMagnus H3-Mar-02 21:55 
GeneralRe: Dont seem to work everytime Pin
Mustafa Demirhan4-Mar-02 5:03
memberMustafa Demirhan4-Mar-02 5:03 
GeneralRe: Dont seem to work everytime Pin
Mustafa Demirhan3-Mar-02 21:58
memberMustafa Demirhan3-Mar-02 21:58 
GeneralRe: Dont seem to work everytime Pin
Mustafa Demirhan8-Mar-02 5:17
memberMustafa Demirhan8-Mar-02 5:17 
GeneralRe: Dont seem to work everytime Pin
Philippe Lhoste18-Mar-02 0:21
memberPhilippe Lhoste18-Mar-02 0:21 
GeneralRe: Dont seem to work everytime Pin
Mustafa Demirhan28-Mar-02 9:55
memberMustafa Demirhan28-Mar-02 9:55 
GeneralSuperb! Pin
Anonymous1-Mar-02 2:20
memberAnonymous1-Mar-02 2:20 
GeneralRe: Superb! Pin
Mustafa Demirhan1-Mar-02 14:32
memberMustafa Demirhan1-Mar-02 14:32 
GeneralRe: Superb! Pin
Philippe Lhoste18-Mar-02 0:25
memberPhilippe Lhoste18-Mar-02 0:25 
GeneralRe: Superb! Pin
Mustafa Demirhan28-Mar-02 9:56
memberMustafa Demirhan28-Mar-02 9:56 
GeneralExcellent! Pin
Philippe Lhoste27-Feb-02 0:16
memberPhilippe Lhoste27-Feb-02 0:16 
GeneralRe: Excellent! Pin
Mustafa Demirhan28-Feb-02 21:10
memberMustafa Demirhan28-Feb-02 21:10 
GeneralRe: Excellent! Pin
Nish [BusterBoy]28-Feb-02 21:40
memberNish [BusterBoy]28-Feb-02 21:40 
GeneralRe: Excellent! Pin
Mustafa Demirhan1-Mar-02 2:22
memberMustafa Demirhan1-Mar-02 2:22 
GeneralRe: Excellent! Pin
Philippe Lhoste18-Mar-02 0:11
memberPhilippe Lhoste18-Mar-02 0:11 
GeneralRe: Excellent! Pin
Mustafa Demirhan28-Mar-02 9:57
memberMustafa Demirhan28-Mar-02 9:57 
GeneralExcuse my ignorance, but... Pin
Stan Shannon21-Feb-02 5:20
memberStan Shannon21-Feb-02 5:20 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 5:58
memberMustafa Demirhan21-Feb-02 5:58 
GeneralRe: Excuse my ignorance, but... Pin
Mustafa Demirhan21-Feb-02 6:07
memberMustafa Demirhan21-Feb-02 6:07 
GeneralRe: Excuse my ignorance, but... Pin
Stan Shannon21-Feb-02 6:57
memberStan Shannon21-Feb-02 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 22 Jun 2004
Article Copyright 2002 by Mustafa Demirhan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid