locked
Trap the Escape Key in a Form RRS feed

  • Question

  • How can I trap the escape key for a form, not for a specific control?

    REvans

    Friday, March 8, 2013 1:00 AM

Answers

  • You can use the form KeyPress or KeyDown events to capture all keyboard input. The Form.KeyPreview property must be set to True as well.
    • Marked as answer by REvans611 Friday, March 8, 2013 1:48 PM
    Friday, March 8, 2013 1:14 AM

All replies

  • You can use the form KeyPress or KeyDown events to capture all keyboard input. The Form.KeyPreview property must be set to True as well.
    • Marked as answer by REvans611 Friday, March 8, 2013 1:48 PM
    Friday, March 8, 2013 1:14 AM
  • REvans611,

    simply drag the small squares

    Friday, March 8, 2013 1:25 AM
  • Thanks, J Chase.  You pointed me in the right direction.  The following is my result which works quiet nicely.

    public myForm()
    {
        InitializeComponent();
        this.KeyDown += new KeyEventHandler(myForm_KeyDown)
        ...
    }
    private void frmClients_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == (char)27)
        {
            this.myControl.Hide();
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
    }


    REvans

    Friday, March 8, 2013 1:48 PM