As developers, we have a tendency to use what worked for us in the past. Overall, there is nothing wrong with this, as long as it still works efficiently, but sometimes we need to change.
Using the ON KEY LABEL for FoxPro 2.6 forms worked pretty well and generally returned the desired results. However, using the ON KEY LABEL in an object environment is a different story. A better approach to using ON KEY LABEL on forms is the KEYPRESS Event.
First Example
You have a form and you wish to recalculate all values (must run a program or method) when the user presses the F9 key. If you look in the help (under INKEY) you will see all the key values. F9 has a value of -9.
Step 1.
Set the KEYPREVIEW property on the form to .T.
Step 2.
Add the following code in the form KEYPRESS event.
IF LASTKEY() = -9
DO recalc.prg
ENDIF
Second Example
You have a login form (or a search form) and you want it to click the [Ok] button after the user presses [Enter] on the password text-field. The default behavior is skipping to the [Ok] button, and then you need to press [Enter] a 2nd time. In addition, you wish to trap to see if the user pressed the [Escape] key on the text-field, and click the [Cancel] button for them if so.
Step 1 is the same.
Set the KEYPREVIEW property on the form to .T.
Step 2
Add the following code in the KEYPRESS event of the password textbox:
IF LASTKEY() = 13
* Enter Key
THISFORM.cmdOK.CLICK()
ENDIF
IF LASTKEY() = 27
* Escape Key
THISFORM.cmdCancel.CLICK()
ENDIF
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.