From RAD Studio API Documentation
Jump to: navigation, search


property OnKeyDown: TKeyEvent read FOnKeyDown write FOnKeyDown;


__property TKeyEvent OnKeyDown = {read=FOnKeyDown, write=FOnKeyDown};


Type Visibility Source Unit Parent
event protected
Vcl.Controls TWinControl


Occurs when a user presses any key while the control has focus.

Use the OnKeyDown event handler to specify special processing to occur when a key is pressed. The OnKeyDown handler can respond to keyboard keys, including function keys and keys combined with the SHIFT, ALT, and CTRL keys, and pressed mouse buttons.

Note: TCheckBox does not fire OnKeyDown events for arrow keys.

OnKeyDown is an event handler of type Vcl.Controls.TKeyEvent. See TKeyEvent for a description of the parameters.

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 CTRL-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.

See Also

Code Examples