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() {
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?
BoltClock Jul 03 '10 at 20:07 2010-07-03 20:07
source share