KeyDown Not Responding Forms

I worked for a while on my Windows Forms project, and I decided to experiment with keyboard shortcuts. After a little reading, I decided that I just had to write an event handler and associate it with the KeyDown event form:

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 this to open the properties panel of the Visual Studio constructor, then double-clicking the KeyDown event of my form to generate a Form1_KeyDown event Form1_KeyDown . But when testing my application, the form does not respond at all to the key combination Ctrl + Alt + O. The Visual Studio designer actually created the code to bind the event handler to the form:

 private void InitializeComponent() { // ... this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); // ... } 

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

In addition, I tried to set a breakpoint on the event binding call (shown above) and found that the program reaches this breakpoint only with a fine. But any breakpoints set in the method definition itself are never reached.

To make sure that I took the first few steps correctly, I tried to repeat them with:

  • New form in the same solution.
    Same problem: the form does not respond when I press Ctrl + Alt + O , and the debugger does not even enter the event handler. Tried it again and it works.

  • New WinForms solution.
    It works fine: a message box appears (the Console.WriteLine() call also works).

So, I'm completely lost. What prevents all forms in this project from receiving KeyDown events?

+44
c # winforms
Jul 03 '10 at 20:07
source share
3 answers

Does your form have the KeyPreview property set to true?

Form.KeyPreview Property

Gets or sets a value indicating whether the form receives the event key before the event is passed to the control with focus.

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

+97
Jul 03 '10 at 20:16
source share

The most common advice on this issue in StackOverflow and MSDN 1 , 2 (including the accepted answer here) is quick and easy:

KeyDown events fire on Form while its KeyPreview property KeyPreview set to true

This is adequate for most purposes, but it is risky for two reasons:

  • KeyDown handlers do not see all the keys . In particular, "you cannot see the types of keystrokes that are used for navigation. Like the cursor keys and Tab, Escape and Enter for dialogue."

  • There are several ways to intercept key events, and they all occur sequentially. KeyDown is the last to be processed. Therefore, KeyPreview not a lot of preview, and the event can be disabled for several stops along the way.

(Credit for @HansPassant for these points.)

Instead, override ProcessCmdKey in 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); } 

Thus, all keys are visible to the method, and the method is first on the line to see the event.

Note that you still have control over whether the focused controls controlled the KeyDown . Just return true to block the subsequent KeyDown , instead of setting KeyPressEventArgs.Handled to true , as it would be in the KeyDown event handler. Here is an article with more detailed information.

+22
Dec 08 '15 at 23:24
source share

Try setting the KeyPreview property in your form to true. This worked for me to register keystrokes.

+17
Jul 03 '10 at 20:18
source share



All Articles