Another way:
1) set the Main Form 'KeyPreview property to 'true.
2) define a 'KeyDown event handler bound to the Main Form window:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
But,
should you do this ? This will close the app, and any other Forms you are displaying. It will not work in whatever shown modally that has focus.
It's not typical Windows app behavior, and may confuse others.
And, there are cases where you really should clean-up/dispose of certain objects you may have created. Give the app user a choice:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (MessageBox.Show("exit application?", "think about it !", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}
}