Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forms not responding to KeyDown events

Tags:

c#

.net

winforms

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?

like image 755
BoltClock Avatar asked Jul 03 '10 20:07

BoltClock


People also ask

Is Keydown an event?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What is the difference between the Keydown and KeyUp events?

The onKeyDown event is triggered when the user presses a key. The onKeyUp event is triggered when the user releases a key.

How do you use Keydown function?

The keydown() is an inbuilt method in jQuery which is used to trigger the keydown event whenever User presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. So, Using keydown() method we can detect if any key is on its way down.


1 Answers

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

like image 178
STO Avatar answered Sep 21 '22 00:09

STO