KeyPress, KeyPreview
The KeyPress event lets you take action as soon as a user presses a key. When combined with the KeyPreview property, you can act at the form level when a key is pressed on any control.
Usage |
PROCEDURE oObject.KeyPress LPARAMETERS [ nControlIndex, ] nKeyPressed, nModifiers |
Parameter |
Value |
Meaning |
nControlIndex |
Numeric |
When a control is a member of a control array, nControlIndex is passed, indicating which control in the array received the keypress. |
Omitted |
The control receiving the keypress is not a member of a control array. |
|
nKeyPressed |
Numeric |
The code for the key that was pressed. The codes passed by KeyPress are the same as those returned by INKEY(). |
nModifiers |
Numeric |
A code indicating whether any of Shift, Ctrl, or Alt were held down when the key was pressed. Each modifier has a value (Shift=1, Ctrl=2, Alt=4). nModifiers is the sum of the values for whichever of the modifier keys were held down. |
IF INLIST(nKeyCode, 98, 66, 2, 48) && Check for a "b"Frankly, we think it would make more sense to always pass the unshifted value and let you figure it out with nModifiers. But the codes reflecting the modifier keys do make KeyPress work more like INKEY(), so it was probably done this way to make the transition to KeyPress easier.KeyPress doesn't catch keystrokes that are valid menu shortcuts. For example, with the default system menu, pressing Ctrl+A in a text box selects all the text in the box and doesn't fire the KeyPress event.
Example |
* To see how KeyPress works, try the following: * Create a form in the Form Designer. * Add a text box to the form. * Put the following code in the KeyPress event of the text box: WAIT WINDOW "Key = " + ALLTRIM(STR(nKeyCode)) + CHR(13) + ; "Modifier = " + ALLTRIM(STR(nShiftAltCtrl)) * Run the form and try typing different keys and key * combinations into the textbox |
Usage |
oForm.KeyPreview = lValue lValue = oForm.KeyPreview |
A form's KeyPress event isn't fired, even with KeyPreview turned on, if an ActiveX control on the form has focus and it has a KeyPress method. This isn't a bug; it's just a fact of life when dealing with ActiveX controls, which have their own event models. |
This one, on the other hand, is a bug: Custom code in KeyPress isn't executed for Alt-key combinations. If you turn event tracking on, you'll see that the KeyPress event fires, but the code in the KeyPress method doesn't execute. This is understandable for Alt-key combinations used as menu hot keys (such as Alt+F for the File menu)—which don't fire the KeyPress event at all—but not for unassigned combinations. |
Example |
* The form should have a property lChanged * Set KeyPreview=.t. * The form level KeyPress contains: IF UPPER(this.ActiveControl.BaseClass) $ "TEXTBOX,EDITBOX" ; AND NOT INLIST(nKeyCode, 9, 13, 15, 271) * We're only interested in input keystrokes, * not keystrokes on buttons, etc. * We also only want to recognize "data" keystrokes, * so we omit Tabs, Returns, etc. * The list to omit could also include arrow keys and * other navigation keys. this.lChanged=.T. ENDIF * Now, the form's QueryUnload method * can contain code like: LOCAL nChoice IF thisform.lChanged * Prompt the user to save changes. nChoice = MESSAGEBOX("Save changes", ; MB_YESNOCANCEL + MB_ICONQUESTION + MB_DEFBUTTON1) DO CASE CASE nChoice = IDYES * Save the current record. CASE nChoice = IDCANCEL * Kill the form close. NODEFAULT ENDCASE ENDIF |
See Also |