111

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?

0

4 Answers 4

221

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

3
  • 23
    It's a hack, made available to keep VB6 programmers happy. It has order-of-execution problems, override ProcessCmdKey() instead. Commented Jul 3, 2010 at 21:31
  • @HansPassant, I can't find anything explaining the order-of-execution problems. KeyDown + KeyPreview won't see all keys, which is enough of a problem, but what are the execution order problems?
    – kdbanman
    Commented Dec 8, 2015 at 23:11
  • 1
    There are lots of overrides to detect shortcut keystrokes. Executed in order, KeyPreview + KeyDown is dead last. Commented Dec 8, 2015 at 23:43
75

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.

2
  • 2
    This is the correct answer, especially if you find PreviewKeyDown won't fire at all with KeyPreview set to true.
    – Tim
    Commented Jan 27, 2016 at 23:16
  • This is good, but not straightforward if you need to do something async.
    – rory.ap
    Commented Jan 13 at 17:15
26

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

0
0

Put these within: public [formName]() { }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.