Control.KeyDown Event

 

Occurs when a key is pressed while the control has focus.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

member KeyDown : IEvent<KeyEventHandler,
    KeyEventArgs>

Key events occur in the following order:

  1. KeyDown

  2. KeyPress

  3. KeyUp

To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true. Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true. Instead of overriding the IsInputKey method, you can handle the PreviewKeyDown event and set the IsInputKeyproperty to true. For a code example, see the PreviewKeyDown event.

For more information about handling events, see Handling and Raising Events.

The following code example uses the KeyDown event to determine the type of character entered into the control.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top
Show: