• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1527
  • Last Modified:

C# winform press any key


I  have a winform app, how can write code to handle any key press to exit my program?

1 Solution
Have you tried handling the KeyPress event?

private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
      // Do stuff here
MSDN has a good discussion on this and below is a code excerpt. URL: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.71).aspx

// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;

Open in new window

This discussion was also informative and showed a different way to approach things:

Mike TomlinsonMiddle School Assistant TeacherCommented:
You may need to set the Forms KeyPreview() Property to True to get the KeyPress() event to work properly (depends on which controls you have on your form).

To close the app, from the main form, use "this.Close();".
Naman GoelSoftware engineer 1Commented:
set form's KeyPreview property to true.

this.KeyPreview = true;

and handle any of these event

KeyDown(object sender, KeyEventArgs e)

KeyPress(object sender, KeyPressEventArgs e)

KeyUp(object sender, KeyEventArgs e)

like this :

 private void TestForm_KeyDown(object sender, KeyEventArgs e)
jtran007Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now