0

Pressing Esc is not firing the KeyDown event. Pressing Spacebar or F fires the event but Escdoes not.

What may be the reason?

KeyPreview is set to True in the following example:

    private void form_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Space:
                btnCreate_Click(null, null); break;
            case Keys.F:
                switchThrow(); break;
            case Keys.Escape:
                form_FormClosing(null, null); break;
        }
    }
Tahirhan
  • 352
  • 1
  • 7
  • 15
  • 3
    What's the point of this code? Why is the code calling the *event handlers* of other controls instead of methods? In any case the `ESC` key has a very specific job - cancel and close the form. Submitting it is the job of `Enter`. This code can result in a lot of confusion in end users *and* the form code itself – Panagiotis Kanavos Oct 16 '20 at 06:31
  • Instead of clicking on Close button with the mouse, we just want to be able to close the form by pressing Esc, that's the point of the code. Called event handler methods are doing what the case needs so I called them directly, but I should define contents of them as a separate method I know. – Tahirhan Oct 16 '20 at 06:55
  • 1
    You can already do that if you configure the form properly, as `Esc` is the keystroke for Cancel, the same way `Enter` is the keystroke for `OK` – Panagiotis Kanavos Oct 16 '20 at 07:05
  • 1
    Check [How to: Designate a Windows Forms Button as the Cancel Button](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-designate-a-windows-forms-button-as-the-cancel-button?view=netframeworkdesktop-4.8#:~:text=On%20any%20Windows%20Form%2C%20you,the%20form%20has%20the%20focus.) and the same [using the designer](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/designate-a-wf-button-as-the-cancel-button-using-the-designer?view=netframeworkdesktop-4.8) – Panagiotis Kanavos Oct 16 '20 at 07:05
  • 1
    Also [How to: Designate a Windows Forms Button as the Accept Button Using the Designer](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/designate-a-wf-button-as-the-accept-button-using-the-designer?view=netframeworkdesktop-4.8), which by default will use ENTER as the Accept button. – Panagiotis Kanavos Oct 16 '20 at 07:10
  • Thanks a lot! I didn't know that, but weirdly after adding the Cancel button and defining it as a CancelButton to form pressing Esc is not firing the btnCancel_Click method and it is linked to the button's Click event. I guess I am using the wrong event handler, KeyPress and PreviewKeyDown events didn't help either. – Tahirhan Oct 16 '20 at 07:21
  • " A cancel button is clicked whenever the user presses the ESC key" says in the document so the Click event should work. – Tahirhan Oct 16 '20 at 07:32

1 Answers1

0

Makes sense. Check the documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keydown?view=netcore-3.1

Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form.

    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Escape)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }

Instead of overriding the IsInputKey method, you can handle the PreviewKeyDown event and set the IsInputKey property to true. For a code example, see the PreviewKeyDown event.

button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Escape:
            e.IsInputKey = true;
            break;
    }
}
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61