If you set the Form's 'KeyPreview Property to 'true, then it will handle a Key Event before any of the Form's Controls get the Key Event.
I would strongly suggest ... since you are doing something which is not quite standard Win UI design ... that you build in a safety mechanism so the user doesn't cancel the Application ... possibly losing data:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if
(
MessageBox.Show
(
"Quit the Application",
"Exit Application Dialog",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2
)
== DialogResult.Yes
)
{
Application.Exit();
}
}
}
I prefer to use the KeyUp Event. In "real-world code" I'd would be implementing some level of warning related to whether or not the user had saved their work !