Vcl.Controls.TWinControl.OnKeyPress

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

property OnKeyPress: TKeyPressEvent read FOnKeyPress write FOnKeyPress;

C++

__property TKeyPressEvent OnKeyPress = {read=FOnKeyPress, write=FOnKeyPress};

Properties

Type Visibility Source Unit Parent
event protected
Vcl.Controls.pas
Vcl.Controls.hpp
Vcl.Controls TWinControl

Description

Occurs when a key is pressed.

Use the OnKeyPress event handler to make something happen as a result of a single character key press.

The Key parameter in the OnKeyPress event handler is of type Char; therefore, the OnKeyPress event registers the ASCII character of the key pressed. Keys that do not correspond to an ASCII Char value (SHIFT or F1, for example) do not generate an OnKeyPress event. Key combinations (such as SHIFT+A) generate only one OnKeyPress event (for this example, SHIFT+A results in a Key value of "A" if Caps Lock is off). To respond to non-ASCII keys or key combinations, use the OnKeyDown or OnKeyUp event handler.

An application gets Windows WM_KEYDOWN messages for all keys when the user presses a key. These messages indirectly fire the OnKeyDown event. Setting the Key parameter to #0 prevents any further processing of this message. But for keys that generate characters Windows also produces WM_CHAR. At the time your OnKeyDown event fires, the WM_CHAR message for the key will already be in the message queue. Setting Key to #0 does not stop it from being delivered, so it fires the OnKeyPress event. If you set the Key to #0, OnKeyPress will be prevented from being fired only for keys that do not have chars. For keys that represent characters, OnKeyPress will continue to be fired.

This method of organizing key processing has advantages. Code that only deals with characters, including control characters like #13 for carriage return, #3 for CTROL-C, and so on, should go into the OnKeyPress event. Code that deals with keys that do not generate characters should be put into the OnKeyDown event.

OnKeyPress is an event handler of type Vcl.Controls.TKeyPressEvent.

See Also

Code Examples