Click to See Complete Forum and Search --> : How to create a macro facility for an application?


Luan Bach
March 30th, 1999, 09:40 AM
Hello,


I'm after C++ examples of how to allow the user to record mouse and keyboard events inside an application. The recorded events can then be saved to a file so that they can be play-back at a later date. A macro facility in other words. Can anyone help me with this? I'm using Visual C++ 6 under Windows NT.


Thanks lots.


Luan.

Dave Lorde
March 31st, 1999, 04:52 AM
One way I've seen for doing this kind of thing involves keeping a record of the user activities as a series of commands rather than recording raw mouse and keyboard input.


The application is designed with a Command class hierarchy, so that every user activity creates an instance of a particular command type (e.g. text edit command, delete entry command, etc.) which contains any data required (e.g. user entered text), and can then be executed, if necessary, via a Do or Execute method, to carry out the activity whenever required. Commands can be stored in a list when recording, and played back later by running down the list and executing every command on it. This can be neatly encapsulated as a MacroCommand class. For storage to file, serialization can be used, either in raw binary format, or you could use a tagged text format (XML perhaps?) so it is human-readable.


The command class approach also allows you to easily implement an Undo/Redo stack if undo-able commands have an Undo method that reverses their action. The Command pattern is documented in the GOF patterns book ("Design Patterns - Elements of Reusable Object Oriented Software" by Gamma, Helm, Johnson, and Vlissides, pub. Addison Wesley). This invaluable book also relates this to other patterns that can be used in conjunction with Command.


You may feel this approach is overkill for your requirements, but it's worth having a look at, because once you've implemented the basic design, adding new commands is relatively trivial, and it makes the whole application interface very structured and controllable (and elegant!).


Dave

Luan Bach
March 31st, 1999, 07:09 AM
Thanks Dave. I've though about the method you suggested and have done something similar for my undo/redo functions. But by recording the mouse and keyboard messages I also hoped to provide tutorial macros so that the user can run a macro and see the steps needed to perform a task. Also to provide support so that users can record what they are doing to genreate errors etc.


Luan.