KeyPress Event

Occurs when the user presses and releases a key. You can use the KeyPress event for intercepting keystrokes entered in a control. You can also test keystrokes immediately for validity or format characters as they are typed. Use the KeyPreview property to create global keyboard-handling routines.

PROCEDURE Object.KeyPress
LPARAMETERS nKeyCode, nShiftAltCtrl


  • nKeyCode
    Contains a number that identifies the key pressed. For a list of codes for special keys and key combinations, see INKEY( ).
  • nShiftAltCtrl
    Sets a particular bit if a modifier key is held down while pressing the key specified by nKeyCode. Valid modifier keys are the SHIFT, CTRL, and ALT keys.

    The following table lists the modifier key values for nShiftAltCtrl.

    nShiftAltCtrl Modifier key







    This parameter is the sum of the bits with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2). These bits correspond to the values 1, 2, and 4, respectively. This parameter indicates the state of these keys. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys is pressed. For example, if both CTRL and ALT are pressed, the value of nShiftAltCtrl is 6.


Applies To: CheckBox | ComboBox | CommandButton | EditBox | Form | Grid | ListBox | OptionButton | Spinner | TextBox

The KeyPress event does not occur for any combination of keys with the ALT key.

The object with the focus receives the KeyPress event.

A form can receive the KeyPress event in three special cases:

  • The form contains no controls, or none of its controls is visible and enabled.

  • The form's KeyPreview property is set to True (.T.). The form first receives the KeyPress event, and then the control with focus receives the event.

  • The control on the form cannot process a keystroke, for example, when TAB is pressed to move the focus to the next control.

If the AllowCellSelection property for a grid is set to True (.T.), Visual FoxPro disregards the Grid KeyPress event and uses the event at the individual cell level.

See Also


INKEY( ) Function
KeyPreview Property

Other Resources

Events (Visual FoxPro)
Language Reference (Visual FoxPro)