Click here to Skip to main content
11,411,128 members (66,029 online)
Click here to Skip to main content

Tagged as

Close Application at Press of Escape Button

, 10 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to close Form at pressing of Escape Button.

There are various ways to close an application by pressing the Escape button, using C#. Here are a few:

  • Way 1: Simply add the below given code in your Windows program:
  • protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        if (keyData == Keys.Escape) this.Close();
            bool res = base.ProcessCmdKey(ref msg, keyData);
        return res;
  • Way 2: Write the below code in the KeyPress event of a form:
  • if (e.KeyChar == (char)27)
  • Way 3: Select properties of Form and select 'KeyPreview' and change it from 'false' to 'true'. [By default, its value is false.] Then write the below code in the KeyUp event of the Form:
  • private void Form1_KeyUp(object sender, KeyEventArgs e)
        if (e.KeyCode == Keys.Escape)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.

He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..

He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

GeneralI'm unsure that these protect against having other keys pres... PinmemberPIEBALDconsult18-Jan-12 14:24 
GeneralReason for my vote of 2 CancelButton should be used instead.... PinmemberPhilippe Mori5-Jan-12 7:49 
GeneralYou are Welcome :) PinmvpRaviRanjankr6-Jul-11 19:06 
GeneralReason for my vote of 5 I had to do the hard way... create ... PinmemberBlubbo6-Jul-11 3:30 
GeneralRe: You are Welcome :) PinmvpRaviRanjankr6-Jul-11 19:06 
GeneralClosing a Windows application when ESC is pressed is not nec... PinmemberPablo Aliskevicius5-Jul-11 3:42 
GeneralRe: Thanks Pablo for your Feedback. PinmvpRaviRanjankr6-Jul-11 19:04 
GeneralReason for my vote of 5 interesting PinmemberMember 76920994-Jul-11 22:54 
GeneralRe: Thank you. PinmvpRaviRanjankr5-Jul-11 1:28 
GeneralReason for my vote of 4 Good options PinmemberAll Time Programming4-Jul-11 21:12 
GeneralRe: Thanks :) PinmvpRaviRanjankr4-Jul-11 21:38 
GeneralReason for my vote of 5 nice PinmemberPritesh Aryan2-Jul-11 0:35 
GeneralRe: Thanks Pritesh. PinmvpRaviRanjankr2-Jul-11 1:12 
SuggestionYou don't need all this... PinsubeditorIndivara29-Jun-11 16:29 
GeneralRe: You don't need all this... PinmemberRaviRanjankr30-Jun-11 3:18 
GeneralRe: You don't need all this... PinsubeditorIndivara1-Jul-11 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 11 Nov 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid