Click here to Skip to main content
13,452,837 members (56,041 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked
Posted 29 Jun 2011

Close Application at Press of Escape Button

, 10 Nov 2011
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
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionStop the macro playback Pin
burgo8553-Dec-16 18:25
memberburgo8553-Dec-16 18:25 
GeneralI'm unsure that these protect against having other keys pres... Pin
PIEBALDconsult18-Jan-12 13:24
memberPIEBALDconsult18-Jan-12 13:24 
GeneralReason for my vote of 2 CancelButton should be used instead.... Pin
Philippe Mori5-Jan-12 6:49
memberPhilippe Mori5-Jan-12 6:49 
GeneralYou are Welcome :) Pin
RaviRanjankr6-Jul-11 18:06
mvpRaviRanjankr6-Jul-11 18:06 
GeneralReason for my vote of 5 I had to do the hard way... create ... Pin
Blubbo6-Jul-11 2:30
memberBlubbo6-Jul-11 2:30 
GeneralRe: You are Welcome :) Pin
RaviRanjankr6-Jul-11 18:06
mvpRaviRanjankr6-Jul-11 18:06 
GeneralClosing a Windows application when ESC is pressed is not nec... Pin
Pablo Aliskevicius5-Jul-11 2:42
memberPablo Aliskevicius5-Jul-11 2:42 
GeneralRe: Thanks Pablo for your Feedback. Pin
RaviRanjankr6-Jul-11 18:04
mvpRaviRanjankr6-Jul-11 18:04 
GeneralReason for my vote of 5 interesting Pin
Member 76920994-Jul-11 21:54
memberMember 76920994-Jul-11 21:54 
GeneralRe: Thank you. Pin
RaviRanjankr5-Jul-11 0:28
mvpRaviRanjankr5-Jul-11 0:28 
GeneralReason for my vote of 4 Good options Pin
All Time Programming4-Jul-11 20:12
memberAll Time Programming4-Jul-11 20:12 
GeneralRe: Thanks :) Pin
RaviRanjankr4-Jul-11 20:38
mvpRaviRanjankr4-Jul-11 20:38 
GeneralReason for my vote of 5 nice Pin
Pritesh Aryan1-Jul-11 23:35
memberPritesh Aryan1-Jul-11 23:35 
GeneralRe: Thanks Pritesh. Pin
RaviRanjankr2-Jul-11 0:12
mvpRaviRanjankr2-Jul-11 0:12 
SuggestionYou don't need all this... Pin
Indivara29-Jun-11 15:29
subeditorIndivara29-Jun-11 15:29 
GeneralRe: You don't need all this... Pin
RaviRanjankr30-Jun-11 2:18
memberRaviRanjankr30-Jun-11 2:18 
GeneralRe: You don't need all this... Pin
Indivara1-Jul-11 1:01
subeditorIndivara1-Jul-11 1:01 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180318.3 | Last Updated 11 Nov 2011
Article Copyright 2011 by RaviRanjanKr
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid