digdug

hi! i would like to capture control + M as a shorcut key in opening a form

but it is not working.. what is wrong with this code

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

if(e.KeyData == (Keys.Control | Keys.M)) {

Form2 form2 = new form2();

form2.ShowDialog();

}

}




Re: Windows Forms General keydown event for control + any key

Andreas Johansson

Have you set the KeyPreview property to true
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

If it is not set to true the currently active control will receive the event and most likely stop it from getting to the form. Setting KeyPreview to true will ensure the form gets the event first and can take action on shortcut keys.

If you handle the specific event you should set e.Handled to true.






Re: Windows Forms General keydown event for control + any key

Ronaldo Ferreira

if (e.Control && e.KeyCode == Keys.M)

the code above worked fine in my application.





Re: Windows Forms General keydown event for control + any key

Andreas Johansson

Ronaldo Ferreira wrote:

if (e.Control && e.KeyCode == Keys.M)

the code above worked fine in my application.

Even if you add a control like TextBox to the form and have it focused when pressing the key combination






Re: Windows Forms General keydown event for control + any key

Ronaldo Ferreira

Andreas Johansson wrote:
Ronaldo Ferreira wrote:

if (e.Control && e.KeyCode == Keys.M)

the code above worked fine in my application.

Even if you add a control like TextBox to the form and have it focused when pressing the key combination

No, the UserControl must be focused. In my application, the user must click the control before using any key combinations...





Re: Windows Forms General keydown event for control + any key

Andreas Johansson

Ronaldo Ferreira wrote:

No, the UserControl must be focused. In my application, the user must click the control before using any key combinations...

If you want to have shortcut keys for all control on a form you should set KeyPreview to true and handle the KeyDown event for the form instead on the control.






Re: Windows Forms General keydown event for control + any key

Ronaldo Ferreira

Andreas Johansson wrote:
Ronaldo Ferreira wrote:

No, the UserControl must be focused. In my application, the user must click the control before using any key combinations...

If you want to have shortcut keys for all control on a form you should set KeyPreview to true and handle the KeyDown event for the form instead on the control.

Yes, you're right. But in my case I wanted that every UserControl work by "itself". I have several controllers that have the same hotkeys. The user must select the control that he/she wants to use.

thanks for answering me.





Re: Windows Forms General keydown event for control + any key

nobugz

Ctrl+M generates the same KeyPress code as the Enter key. That's obvious when a multi-line TextBox has the focus. Some backwards MS-Dos compatibility getting in the way, I'd guess. Ctrl+H and Ctrl+J work too (backspace and linefeed). Still, setting KeyPreview = True ought to solve the OP's problem.