Forms not responding to KeyDown events

C#.NetWinforms

C# Problem Overview


I've been working for a while on my Windows Forms project, and I decided to experiment with keyboard shortcuts. After a bit of reading, I figured I had to just write an event handler and bind it to the form's KeyDown event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.Alt && e.KeyCode == Keys.O)
    {
        MessageBox.Show("Ctrl+Alt+O: magic!");
    }
}

I did that the good ol' way of opening the Properties panel of the Visual Studio designer, then double-clicking on the KeyDown event of my form to generate the Form1_KeyDown event handler. But on testing my application, the form doesn't respond at all to the Ctrl+Alt+O keyboard shortcut. The Visual Studio designer did generate the code to bind the event handler to the form though:

private void InitializeComponent()
{
    // ...

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

    // ...
}

So I tried adding a Console.WriteLine() call to the handler to check that it was being called at all, but no luck on that either.

Also, I tried to set a breakpoint on the event binding call (shown just above) and found that the program reaches that breakpoint just fine. But any breakpoints I set within the method definition itself are never reached.

To make sure I was doing the first few steps correctly, I tried repeating them with:

  • A new form in the same solution.
    Same issue: the form doesn't respond when I press my Ctrl+Alt+O keyboard shortcut and the debugger isn't even stepping into the event handler. Tried this again and it works.

  • A brand new WinForms solution.
    It works perfectly: the message dialog appears (the Console.WriteLine() call also works).

So I'm quite lost here. What's preventing all the forms in this one project from receiving KeyDown events?

C# Solutions


Solution 1 - C#

Does your form have KeyPreview property set to true?

> Form.KeyPreview Property > > Gets or sets a value indicating whether the form will receive key > events before the event is passed to the control that has focus.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Solution 2 - C#

The most common piece of advice for this problem on StackOverflow and the MSDN1, 2 (including the accepted answer here) is quick and easy:

> KeyDown events are triggered on a Form as long as its KeyPreview property is set to true

That's adequate for most purposes, but it's risky for two reasons:

  1. KeyDown handlers do not see all keys. Specifically, "you can't see the kind of keystrokes that are used for navigation. Like the cursor keys and Tab, Escape and Enter for a dialog."

  2. There are a few different ways to intercept key events, and they all happen in sequence. KeyDown is handled last. Hence, KeyPreview isn't much of a preview, and the event could be silenced at a few stops on the way.

(Credit to @HansPassant for those points.)

Instead, override ProcessCmdKey in your Form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (keyData == Keys.Up)
    {
        // Handle key at form level.
        // Do not send event to focused control by returning true.
        return true;
    }
  return base.ProcessCmdKey(ref msg, keyData);
}

That way, all keys are visible to the method, and the method is first in line to see the event.

Note that you still have control over whether or not focused controls see the KeyDown event. Just return true to block the subsequent KeyDown event, rather than setting KeyPressEventArgs.Handled to true as you would in a KeyDown event handler. Here is an article with more details.

Solution 3 - C#

Try setting the KeyPreview property on your form to true. This worked for me for registering key presses.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoltClockView Question on Stackoverflow
Solution 1 - C#STOView Answer on Stackoverflow
Solution 2 - C#kdbanmanView Answer on Stackoverflow
Solution 3 - C#Seb CharrotView Answer on Stackoverflow