Company logo

FOXSTUFF

Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Versión en español de este artículo

Use keyboard macros to speed up repetitive actions

How a little-used Visual Foxpro feature can boost your productivity

Visual Foxpro's development environment offers some pretty powerful tools, but some of them are - how shall we put it? - a little long-winded to use. Take the alignment commands on the Form Designer's Format menu. As you probably know, these let you align a group of controls along their left sides, top edges, horizontal centres, and so forth. You multi-select the controls, then choose Format, then Align, then the appropriate alignment command.

That's easy enough if you only want to perform the action occasionally. But on a typical form, you might have dozens of controls that need lining up, so you could find yourself repeating the same steps many times. This soon becomes tedious. 

It is for that sort of situation that keyboard macros were invented. A macro is simply a sequence of keystrokes that perform a particular task. Once you have created the macro, you assign it to a single keystroke or key combination - usually a function key or a control-letter combo. Then, any time you need to repeat the task, you just hit the key in question.

How to do it

Let's look at the process in a little more detail. As an example, we'll create a macro to align controls along their left sides. Start by opening a form in the Form Designer. Add a couple of controls and multi-select them (for example, by clicking on them while holding down the Shift key). You are now ready to record the macro.

Go to the Tools menu and select Macros. You will see a dialogue like the one in Figure 1. When you click the Record button, you will be prompted for the 'defined key' (that's the key that you want to assign the macro to) and the macro name. To specify the defined key, just press the key or key combination in question.

Figure 1: The Macro dialogue

In general, the defined key can be a function key on its own, or a function key, letter or digit key modified with any combination of Shift, Ctrl or Alt. You will obviously want to avoid keys that clash with existing functions, such as Ctrl-C (which copies text to the clipboard) or F1 (which invokes Help), but that still leaves a good number of combos to choose from.

Having specified the key, you enter a name for the macro - this is a simple name to help you identify the macro if you later want to edit or delete it - then click the OK button.

You are now in macro-recording mode. So go ahead and perform the steps needed to align the controls along their left sides. Specifically, you will press Alt-O (to open the Format menu), A (for Align) and L (for Left Sides). Be sure only to use the keyboard for these steps, as mouse actions are not recorded in macros.

When you have completed the action, go back to the Tools menu and choose Macros again. This time, do use the mouse, otherwise unwanted keystrokes will appear in the macro. You will now see a dialogue like the one in Figure 2. Just click on the OK button to stop recording.

Figure 2: Use this dialogue when you have finished recording your macro

From now on, whenever you want to align a group of controls, just multi-select them and hit the relevant key.

More macros

Alignment commands apart, there are many repetitive processes in Visual Foxpro that lend themselves to the macro treatment, and you will soon build up a personal collection of favourites. Just about any process that can be executed entirely from the keyboard can be made into a macro.

Here are some examples of macros that you might want to record:

Saving the macros

When you record a macro, it exists only in memory. To prevent your macros from being lost when you quit Visual Foxpro, you need to save them in a file. To do so, select Tools / Macros again, click on Save, and specify a filename and folder for the file. The file will have the extension FKY.

The next time you want to use the macros, return to the Macros dialogue, click on Restore, and navigate to the same FKY file. If you want to automate this step (to load the macro file each time you launch VFP), use the RESTORE MACROS command, for example:

RESTORE MACROS FROM c:\dev\MyMacros

This command could be executed from a start-up program launched from a CONFIG.FPW file.

Alternatively, save the macros in a file named DEFAULT.FKY, in the folder from which you launch VFP. This default macro file will be loaded automatically each time you launch Visual Foxpro. A quick way of creating DEFAULT.FKY is simply to click on the Default button in the Macros dialogue.

Editing and deleting

If you make a mistake while recording a macro, you won't necessarily have to cancel it and start again. It is often easier to carry on with the recording, and to correct the error when you have finished. To correct a macro, select it in the Macros dialogue and click on the Edit button. You will then be able to edit the keystroke within the macros. In this edit box, printable keys (letters, digits and symbols) can be edited as plain text, while control keys (function keys, Enter, Tab, etc., as well as Ctrl- and Alt- combos) appear as key names enclosed in curly braces.

The Edit Macro dialogue also lets you change the defined key and the macro name. And when you no longer need a macro, you can delete it by selecting it in the Macros dialogue and pressing Clear.

As you become familiar with macros, you will soon appreciate their advantages. Use them where appropriate, and they could do a great deal to speed up your work.

Mike Lewis Consultants Ltd. September 2001

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.