FunKeys

Replace the sample text with your own text, graphics, and multimedia files. To provide a link to another page, select text, and click Hyperlink on the Insert menu.

Makes the FoxPro Function Keys:

More convenient

Safer

More functional

More Fun!

 

FoxPro is a registered trademark of Microsoft Corporation

MAKING THE FUNCTION KEYS MORE FUNCTIONAL

By Rick Shaddock

Computer Instructors Corporation, Washington, DC

703-486-2222 Fax: 877-727-9070

INTERNET: Rick@CICorp.com

 

INTRODUCTION - Existing Function Keys are not Functional

 

The FoxPro function keys are less than the useful function keys they should be. This is true even in the new Visual FoxPro. This article describes an easy way to make them more functional, both for end-users and developers. End users need a way to quickly perform repetitive tasks and protect data entry. Developers need to be able to display the current status of the environment to help in trouble-shooting, detection, and prevention.

 

The default settings for the FoxPro function keys are a carry-over from the old dBase days when everything was output to the screen or desktop. This is unacceptable in applications using FoxPro windows, where the desktop background is kept clear or only displays a client's logo.

 

The following program, called FUNKEYS.PRG redefines the function keys to be more useful in FoxPro. Indeed, I had completely gotten away from using the function keys before creating this program. The function keys were not very functional, and were even dangerous to data for those doing data entry.

 

Ordinarily, if you press F2 or F5 in the BROWSE mode, your data is overstricken by the letters of "SET" or "DISPLAY STRUCTURE". This can be very annoying to users who might press them by mistake, overwriting important data. You may have seen many times the word "APPEND" appearing in the data files caused by a book or something resting on the user's F9 key. That is why programmers often clear the macros at the start of a FoxPro session or program with CLEAR MACROS.

 

While delivering an application and documentation years ago, a client asked "Does it come with a function key template like WordPerfect?" When I told her it did nothing with the function keys she actually looked disappointed. That started me thinking that there is vast unused potential in those function keys and that many users would like to use them. With this program, the function keys will be as they should be, truly useful.

 

MAKING THE FUNCTION KEYS CONVENIENT AND SAFE FOR END-USERS

 

As you know, the default FoxPro function keys are as follows (the ; indicates an {Enter}):

 

Macro: Keystrokes:

F1 HELP;

F2 SET;

F3 LIST;

F4 DIR;

F5 DISPLAY STRUCTURE;

F6 DISPLAY STATUS;

F7 DISPLAY MEMORY;

F8 DISPLAY;

F9 APPEND;

F10 Menu bar activation

F11 nothing

F12 nothing

 

There is much room for improvement here.

 

It also comes with the following (basically useless) function keys:

Shift+1 {SHIFT+END}

Shift+2 {SHIFT+DNARROW}

Shift+3 {SHIFT+PGDN}

Shift+4 {SHIFT+LEFTARROW}

Shift+5 undefined

Shift+6 {SHIFT+RIGHTARROW}

Shift+7 {SHIFT+HOME}

Shift+8 {SHIFT+UPARROW}

Shift+9 {SHIFT+PGUP}

 

For one thing, these only work on the keybad. Shift+1 makes !, Shift+1 through Shift +9 make "!@#$%^&*(" And even if they did work, you are going to press Shift and a key anyway to move in a direction. So why not just press Shift and that directional key. This must be for prehistoric keyboards without cursor keys.

 

One technique to change the function keys is to use the FoxPro Macros feature. The macros (under System in Fox-DOS DOS or Program in Fox-Windows/Mac) are keyboard macros. The user can define his or her own macros with SHIFT-F10, even in a standalone EXE. These can be very useful for entering repetitive text, such as "Washington;DC;200" to enter the city, state, and first two characters of the zip code. These send keystrokes much as the KEYBOARD command does. But, they cannot make decisions or detect your current variable or field. Macros can overstrike your data if you hit the wrong macro key.

 

ON KEY LABEL commands are similar but different to the Macros function. They are similar because they redefine the actions of function key combinations to automate tasks. But, the ON KEY LABEL commands can be made to be more intelligent by executing programs or making decisions using the IIF function. Macros simply store and replay keystrokes. The default function keys for FoxPro are really Macros which keyboard the keystrokes of the command which in turn execute a command. Macros can INDIRECTLY execute commands. ON KEY LABELs execute the command DIRECTLY and cleanly.

 

The following is part of a program called FUNKEYS.PRG which could be executed upon starting FoxPro or at the beginning of an application:

 

* Key: Command: Comment:

ON KEY LABEL F1 HELP && Help (standard)

ON KEY LABEL F2 SET && View

ON KEY LABEL F3 BROWSE PREFERENCE pBrowse NOWAIT && Browse

ON KEY LABEL F4 DO DispDir IN FunKeys && Directory

ON KEY LABEL F5 DO DispStru IN FunKeys && Structure

ON KEY LABEL F6 DO DispStat IN FunKeys && Status

ON KEY LABEL F7 do DispMemo IN FunKeys && Memvars

ON KEY LABEL F8 CHANGE PREFERENCE pChange NOWAIT && Change

ON KEY LABEL F9 APPEND BLANK && Append

ON KEY LABEL F10 && Menu

ON KEY LABEL F11 && Unused

ON KEY LABEL F12 CLOSE ALL && Close all

 

We will not change the F1 key as that is used by every major program for the Help key (except WordPerfect for DOS). Luckily the default FoxPro F1 macro does not overstrike your data by typing "H E L P". The F2 - F9 macros are not as careful.

 

F2 is set to SET instead of keyboarding the letters "S" "E" "T". Ordinarily, you see the letters "SET" inserted into your get box or field as you are editing or browsing. Now F2 will not overstrike your data. The View Window will immediately pop up showing your current environment.

 

F3 is set to browse instead of LIST. LIST was useful in dBase II, but has diminished in importance as a command. LIST includes no field list, and users usually have more than 5 or 6 fields. So LIST word wraps all over the desktop with a screen dump of the data which is difficult to read. BROWSE is much more useful and easier to read. BROWSE shows a "list" of records in a more convenient, moveable, sizeable window, with the ability to widen and move fields, edit data, and mark records for deletion. BROWSE deserves LIST's place as function key F3.

 

F4 - F7 display the same output as they do in dBase, but in moveable, sizeable windows. They usually display to the desktop, which either cannot be seen within the application, or alter the text currently on the desktop. To get the output to appear in a window instead of the desktop, we will create a temporary text file with no extension, then view it in a window.

 

The following procedures are activated by the function keys F4 - F7:

 

PROCEDURE DispDir

* Display Directory in a window instead of on the desktop

* Called from: ON KEY LABEL F4 DO DispDir IN FunKeys

IF WVISIBLE('Dir')

ACTIVATE WINDOW Dir

ELSE

SET CONSOLE OFF

SET SAFETY OFF

DIR to Dir.

SET SAFETY ON

SET CONSOLE ON

MODIFY FILE Dir. NOWAIT

ENDIF

PROCEDURE DispStru

* Display Structure in a window instead of on the desktop

* Called from: ON KEY LABEL F5 DO DispStru IN FunKeys

IF WVISIBLE('Structur')

ACTIVATE WINDOW Structur

ELSE

SET SAFETY OFF

DISPLAY STRUCTURE NOCONSOLE TO Structur.

SET SAFETY ON

MODIFY FILE Structur. NOWAIT

ENDIF

 

PROCEDURE DispStat

* Display Status in a window instead of on the desktop

* Called from: ON KEY LABEL F6 DO DispStat IN FunKeys

IF WVISIBLE('Status')

ACTIVATE WINDOW Status

ELSE

SET SAFETY OFF

LIST STATUS NOCONSOLE TO Status.

SET SAFETY ON

MODIFY FILE Status. NOWAIT

ENDIF

 

PROCEDURE DispMemo

* Display Memory in a window instead of on the desktop

* Called from: ON KEY LABEL F7 DO DispMemo IN FunKeys

IF WVISIBLE('Memory')

ACTIVATE WINDOW Memory

ELSE

SET SAFETY OFF

LIST MEMORY NOCONSOLE TO Memory.

SET SAFETY ON

MODIFY FILE Memory. NOWAIT

ENDIF

 

The first time you use either F5 through F7 and the window is displayed, it will be word wrapping. It will be easier to read if you turn the word wrap off. Go to Edit Preferences to turn the word wrap off, and save the preferences for files with no extension. You will have files called STRUCTUR, STATUS, and MEMORY from which you can select File Print to get a printout. Set _PEJECT to "AFTER". You can also use these files to copy from and paste into your documentation.

 

F8 is set to CHANGE instead of DISPLAY. DISPLAY simply dumps one record to the desktop--a very boring command. If you have 100 fields, you will see the word wrapping field names, then press any key to see the wordwrapped data. It is difficult to tell which data goes with which field. CHANGE also displays one record, but in an easily read, moveable, sizeable, scrollable window. CHANGE deserves DISPLAY's place as function key F8.

 

F9 has been changed to Append Blank, which will create a new record. You may substitute a special appending program here if you want to stuff fields in the new record with default values or scatter the fields into memory variables to facilitate an Undo function.

 

F10 is not changed because this is used to access Fox's top bar menu.

 

F11 is not used as it may be necessary to keep a function key undefined. Many E-Mail applications need a customizable function key to answer the incoming message.

 

F12 has been defined to CLOSE ALL. It is a good habit for users to close all their databases when going on break. This writes the data to disk, ensuring that not even one field's data would be lost during a power outage. It also closes the file to prying eyes. F12 provides an easy way to do this. And if you make it easy enough, the users might do it.

 

 

DATE & TIME KEYS FOR DATA ENTRY

 

End users may also appreciate handy "quick keys" to enter the date and time. Function key combination Alt-2 could be set for "Today." This keyboards the date without the slashes into date fields. Alt-3 could be set for entering the time, which cannot be done using macros since time is always changing. Since "SET" is not available in a standalone EXE, you may wish to use F2 for the "TO-day" function.

 

* Displaying the Date & Time in a message window

ON KEY LABEL Alt+1 WAIT WINDOW dtoc(date()) +' ' +time() NOWAIT

 

* Entering today's date in a date field

ON KEY LABEL Alt-2 KEYBOARD ;

SUBSTR(DTOC(DATE()),1,2) +;

SUBSTR(DTOC(DATE()),4,2) +;

SUBSTR(DTOC(DATE()),7)

 

* Entering the current time in a character field

ON KEY LABEL Alt-3 KEYBOARD TIME()

 

 

You might also have a program to bring up a pop-up calendar (subject of another article) triggered by a function key such as Alt-4.

 

 

The previous function keys have made the function keys that usually are available to end users safer and more convenient. Safer because they do not overstrike data, and more convenient because they display in moveable, sizeable windows. They also trigger commands and functions which are more often used for data entry, such as BROWSE and CHANGE.

 

 

DIAGNOSTIC FUNCTION KEYS (For Developers)

 

We will now shift to the function keys which can be called the "Diagnostic" function keys. These would be of more interest to FoxPro developers and will use the function keys in combination with Shift. They assist by telling you where you are and what is happening, even during the running of a standalone executable. They have proved invaluable in providing useful information and saving time in trouble-shooting.

 

* Function Keys with Shift - Diagnostics and Information

 

* Context help - we will not change Shift+F1

ON KEY LABEL Shift+F1

 

* Current DBF, Record position, and Object or Field

* Example: [DBF: C:\ABC\CUSTOMER.DBF Rec: 5/500 Obj: COMPANY C 25]

ON KEY LABEL Shift+F2 WAIT WINDOW 'DBF: ' +DBF() +;

' Rec: ' +LTRIM(STR(RECNO())) +;

'/' +LTRIM(STR(RECCOUNT())) +;

' Obj: ' +VARREAD() +' ' +TYPE(VARREAD()) +' ' +;

LTRIM(STR(FSIZE(VARREAD()))) NOWAIT

 

* Current Directory, and FoxPro Path

* Example: [Current: C:\FOXPRO\TUTORIAL Path: C:\FOXPRO;C:\FOXPRO\TOOLS]

ON KEY LABEL Shift+F3 WAIT WINDOW 'Current: ' +SYS(5) +SYS(2003) +;

' Path: ' +TRIM(SET('Path')) NOWAIT

 

* FoxPro version and startup directory

* Example: [FoxPro 2.6 for Windows EXE Directory: C:\FOXPRO]

ON KEY LABEL Shift+F4 WAIT WINDOW version() + ' EXE Directory: ' +;

SYS(2004) NOWAIT

 

* Resource file, ON/OFF, and Printer Setup

* Example: [Resource: C:\ABC\FOXUSER.DBF ON Printer: LaserJet]

ON KEY LABEL Shift+F5 WAIT WINDOW 'Resource: ' +SYS(2005) +' ' +;

SET('Resource') +;

' Printer: ' +_PDSETUP NOWAIT

 

* Current Disk Space usage

* Example: [Disk space: 6,299,648 / 535,552,000]

ON KEY LABEL Shift+F6 WAIT WINDOW 'Disk space: ' +;

LTRIM(TRANS(DISKSPACE(),'###,###,###,###')) +;

' / ' +;

LTRIM(TRANS(VAL(SYS(2020)),'###,###,###,###,###')) NOWAIT

 

* Current Memory usage

* Example: [Memory used/available: 167,008 / 1,310,688 EMS: 0/0]

ON KEY LABEL Shift+F7 WAIT WINDOW 'Memory used/available: '+;

LTRIM(TRANS(VAL(SYS(1016)),'###,###,###')) +;

' / ' +;

LTRIM(TRANS(VAL(SYS(1001)),'###,###,###')) +;

' EMS : '+SYS(23)+'/'+SYS(24) NOWAIT

 

* Current Printer Driver Generator and Printer Driver

* Example: [PDSetup: C:\FPW26\GENPD.APP Driver: LaserJet]

ON KEY LABEL Shift+F8 WAIT WINDOW 'PD Setup: ' +_GENPD +;

' Driver: ' +_PDRIVER NOWAIT

 

* Currently executing Program, Line number and Window on Top

* Example: [Current program: C:\ABC\MAIN.FXP Line: 52 Window: wMAIN]

ON KEY LABEL Shift+F9 WAIT WINDOW 'Current program: '+SYS(16) +;

' Line: ' +LTRIM(STR(LINENO())) +;

' Window: ' +WONTOP() NOWAIT

 

* Define Macro - we will not change this

ON KEY LABEL Shift+F10

 

* Current Filter

* Example: [Filter: STATE="CA"]

ON KEY LABEL Shift+F11 WAIT WINDOW 'Filter: ' +SET('Filter') NOWAIT

 

* Current Index

* Example: [Index: TAG CUSTNUM OF C:\ABC\CUSTOMER.DBF.CDX]

ON KEY LABEL Shift+F12 WAIT WINDOW 'Index: ' +SET('Order') NOWAIT

 

 

The WAIT WINDOW is an easy, non-intrusive way to display information. It creates no new windows or memory variables. The NOWAIT option means that no keystroke is necessary to close the window. Just move the mouse or resume typing in your program. The SUBSTR(), TRIM(), TRANS() functions are used to refine the display of the character strings in the window. The SET() and SYS() functions are the key to display various important information about the status. You can look these up in the manual for details.

 

Example:

---------------------------------------------------------------------

| DBF: C:\TUTORIAL\CUSTOMER.DBF Record: 5/500 Object: CONTACT C 25 |

---------------------------------------------------------------------

You may wish to alter the Diagnostic keys to display multi-line wait windows by inserting a line feed with CHR(13). For example:

 

ON KEY LABEL Shift+F2 WAIT WINDOW 'DBF: ' +DBF() +CHR(13) +;

'Record: ' +LTRIM(STR(RECNO())) +;

'/' +LTRIM(STR(RECCOUNT())) +CHR(13) +;

'Object: ' +VARREAD() +' ' +TYPE(VARREAD()) +' ' +;

LTRIM(STR(FSIZE(VARREAD()))) NOWAIT

Example:

---------------------------------------

| DBF: C:\TUTORIAL\CUSTOMER.DBF |

| Record: 5/500 |

| Object: CONTACT C 25 |

---------------------------------------

 

If the user has a path too long for the wait window you could break up paths longer than 60 characters into two lines using:

ON KEY LABEL Shift+F3 WAIT WINDOW 'Current: ' +SYS(5) +SYS(2003) +;

' Path: ' +IIF(len(trim(SET('Path')))<60,;

SET('Path'), SUBSTR(SET('Path'),1,60) +CHR(13) +;

SUBSTR(SET('Path'),61)) NOWAIT

 

This diplays a wait window to tell you where you are. It works whether you are in in browse, change, or a field in a screen program. In upgrading dBase applications to FoxPro, it is very useful to be able to tell which field you are in with Shift-F2, and which line of which program you are in with Shift-F9.

Executing the FunKeys.prg activates the ON KEY LABELs at some memory cost. The difference between "before" and "after" can be seen with the SYS(1016) function. Or use your new Shift-F7 key!

 

A few more useful Keys for the Developer:

 

In FoxPro for DOS, I like to toggle back and forth between the 25 line per screen mode and 50 lines per screen. During programming, I prefer 50 lines.

 

ON KEY LABEL Alt-F5 DO Set_Disp && VGA 25/50 mode

 

PROCEDURE Set_Disp

* Set Display - Alternate between VGA25 and VGA50

IF NOT _DOS

RETURN .f.

ENDIF

IF SROW() =25

SET DISPLAY TO VGA50

ELSE

SET DISPLAY TO VGA25

ENDIF

* Move the command window to a convenient spot

MOVE WINDOW Command TO SROW() -10,10

 

 

ON KEY LABEL Alt-F11 DO Error.prg WITH -1

 

Alt-F11 is used for viewing an error log contained in ERROR.DBF, to be discussed in another article. This makes a record for every error encountered, including the date, time, DBF, record, object, and a complete listing of status settings and function, Memory variable values, and program calls. This gives you just about all the information FoxPro has to offer about what is happening at the point an error occurs, so you can quickly diagnose and fix it.

 

 

ON KEY LABEL Alt-F12 DO Yn WITH 'Cancel Program?','CANCEL'

 

Alt-F12 could be used to cancel the program if an ultimate way out is needed. This is an unusual keystroke combination, which your users should not know about. The YN is a typical Yes/No function, which will execute the command 'CANCEL' only if they answer "Y".

 

 

Function keys and Alt keys are not the only keys you can define with ON KEY LABEL. You may use the Control key as well. For example, you could use CONTROL-O to bring up a dialog to change the index tag and order of display of the records. But be careful of redefining Control keys because FoxPro already uses many control key combinations for other important functions such as Ctrl-C to Copy and Ctrl-F for Find.

 

 

CONCLUSION

 

The FoxPro function keys should be truly functional keys as their name implies. I hope that, with the FunKeys program, they save you half the time and effort they have saved me in determining the current status and fixing problems. I also hope that they make FoxPro even more fun.

 

Computer Instruction Corporation
Crystal Plaza One
2001 Route One, Suite 211
P.O. Box 2816
Arlington, VA 22202
703-486-2222
877-727-9070 Fax
800-319-3190

CIC@CICorp.com

Return to C I Corporation Software

Return to C I Corporation Home page