Skip to content

Best Way to Close Form with Escape Key

Do you wish to allow your users to exit your application, or close a windows form when they press the Escape key?

Well first of all, it may be really easy for you if you have a custom “Cancel” button already featured on the screen, as you can simply hook that additionally by the Escape key. If this is the case, just simply bind your Cancel button in the CancelButton property featured on your form control and away you go!

However, if you do not have a Cancel button, like me, perhaps for an initial Login screen or some such, you may just wish to bind the Escape key to Close/Quit your application nonetheless..

Well, if you have tried either:

        private void FormLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

or this:

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
                this.Hide();
        }

and found to no avail that the form doesn’t close, it is most likely due to the fact you currently do not have the window itself in focus, which is hard and annoying to do, plus not desirable for your users. You will most likely want your form to close all the time, no matter what control is selected when you press the Escape key right?

Well this approach works a treat, will handle the raw key command and you just need to plug it in to your win form code behind:

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            try
            {
                if (msg.WParam.ToInt32() == (int)Keys.Escape)
                {
                    this.Close();
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Key Overrided Events Error:" + Ex.Message);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
VN:F [1.9.10_1130]
Rating: 5.0/5 (6 votes cast)
VN:F [1.9.10_1130]
Rating: +5 (from 5 votes)
Best Way to Close Form with Escape Key, 5.0 out of 5 based on 6 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

5 Comments

  1. Nice post. Also see:

    How to trap keystrokes in controls by using Visual C#
    http://support.microsoft.com/kb/320584

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 03-Sep-09 at 2:54 pm | Permalink
  2. Luu Tai

    It’s so easy. :o
    Form1.KeyPreview =true;
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode==Keys.Escape )
    {
    this.Close();
    }
    }

    VA:F [1.9.10_1130]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 15-Dec-09 at 1:53 pm | Permalink
  3. Graham O'Neale

    You mean like in my first example?
    It doesn’t always work.

    VN:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 15-Dec-09 at 3:52 pm | Permalink
  4. hasan

    ;-) Well the third one is working great… Ty

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 12-Jan-11 at 6:04 am | Permalink
  5. dude

    Great solution, thank you!

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 24-May-11 at 4:11 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...