C # keyboard response in Form

Source: Internet
Author: User

To meet the project requirements, add the keyboard ESC response to the full screen Form. The effect is to press the Escape key on the keyboard in the full screen, and the program ends.

I thought it was quite simple, but it took a while to solve it. The procedure is summarized as follows:

First, add this. KeyPress + = new System. Windows. Forms. KeyPressEventHandler (this. Form_KeyPress) under the designer of form );

Then write the response function.


[Csharp]
Private void Form_KeyPress (object sender, KeyPressEventArgs e)
{
If (e. KeyChar = (char) Keys. Escape ){
Application. Exit ();
}

}

Private void Form_KeyPress (object sender, KeyPressEventArgs e)
{
If (e. KeyChar = (char) Keys. Escape ){
Application. Exit ();
}

}
After this is done, run the program and find that the form does not respond to ESC.

You also need to set the KeyPreview attribute to True in form ,:

 

After all, form responds to ESC normally.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.