Keyboard events occur when you type on a keyboard, or when keystrokes are sent using the SendKeys action or the SendKeys statement.
Event | Event property | It occurs |
---|---|---|
KeyDown | OnKeyDown (forms, controls) | When you press any key on the keyboard while a control or form has the focus. A form can have the focus only if all visible controls on the form are disabled, or there are no controls on the form.
The KeyDown event also occurs if you send a keystroke to a form or control using the SendKeys action in a macro or the SendKeys statement in Visual Basic. A form also receives all KeyDown events (even those for controls) before they occur for the controls if you set the form's KeyPreview property to Yes. If you hold down a key, the KeyDown event occurs repeatedly. |
KeyPress | OnKeyPress (forms, controls) | When you press and release a key or key combination that produces a standard ANSI character while a control or form has the focus. A form can have the focus only if all visible controls on the form are disabled, or there are no controls on the form.
For example, you can use the KeyPress event to respond to keystrokes entered in a text box or combo box. For keystrokes not recognized by KeyPress, such as function keys or navigation keys, use the KeyDown and KeyUp events. The KeyPress event also occurs if you send a keystroke that produces a standard ANSI character to a form or control using the SendKeys action in a macro or the SendKeys statement in Visual Basic. A form also receives all KeyPress events (even those for controls) before they occur for the controls if you set the form's KeyPreview property to Yes. If you hold down a key, the KeyPress event occurs repeatedly. |
KeyUp | OnKeyUp (forms, controls) | When you release a pressed key while a control or form has the focus. The object with the focus receives all the keystrokes. A form can have the focus only if all visible controls on the form are disabled, or there are no controls on the form.
The KeyUp event also occurs if you send a keystroke to a form or control using the SendKeys action in a macro or the SendKeys statement in Visual Basic. A form also receives all KeyUp events (even those for controls) before they occur for the controls if you set the form's KeyPreview property to Yes. If you hold down a key, the KeyUp event occurs after all the KeyDown and KeyPress events have occurred. |