Close Form When Escape Key Is Pressed


To cause a Windows Form to close (and the application to exit) when you press the Escape key, simply ensure that your form's KeyDown event handler looks like the following:


 private void Form1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyCode == Keys.Escape) {
            Application.Exit();
        }
}