The reason is that when a key is pressed it will go to the control which has focus on the form, as the
KeyPreview
property of
Form
is set to
False
by default. Let us say the cursor is in a
TextBox
. Now, if the
F3
key is pressed it will not go to the
Form's
KeyDown
event and instead it is captured by the
TextBox
and the KeyDown event of
TextBox
fires.
So, to enable the
KeyDown
event of
Form
even when the focus is on any other control, then the
KeyPreview
property of the
Form
has to be set to
true
as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[
^]
KeyPreview Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
and under remarks as
To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.
After setting
KeyPreview
property the Form's
KeyDown
event will fire first, then the
KeyDown
event of the control which has focus will be fired if the
KeyPressEventArgs.Handled
is not set to true as stated above.