14
votes

i thought i solved this problem by myself but it came back to haunt my application so here it goes:

i have the following keydown event handler registered in a form with a couple of disabled and readonly textboxes and they are only simple shortcuts for the buttons:

private void AccountViewForm_KeyDown(object sender, KeyEventArgs e)
{
    //e.SuppressKeyPress = true;
    //e.Handled = true;
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.E && !isInEditMode)
        btnEditMode_Click(sender, e);
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S && isInEditMode) btnEditMode_Click(sender, e);
    if (e.KeyCode == Keys.Escape) btnCancel_Click(sender, e);
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.W) Close();
}

the form has KeyPreview set to true but whenever a readonly textbox has focus and i press Ctrl + E i can't get "Control.ModifierKeys == Keys.Control" and "e.KeyCode == Keys.E" to be both true at the same time. What is really strange is that Ctrl + W works. Anyone has any idea what the hell is going on? :(

2
I realize this is old. Your code references "Control.ModifierKeys". It should probably be "e.Modifiers".Metro

2 Answers

23
votes

According to this question and this one, It looks like a more general way to handle keyboard shortcuts is to override the ProcessCmdKey() method:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  if (keyData == (Keys.Control | Keys.F)) {
    MessageBox.Show("What the Ctrl+F?");
    return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
}

Have you considered using Alt + E and Alt + S and just setting the mnemonic property for your buttons? That seems to work well for me, and it's easier to set up.

0
votes

I had the same problem

In my application the shortcuts did not work if the form was opened with an invoked show(). They did work if the form was opened with ShowDialog(). I also found that the keydown event for the textbox was not fired by CTRL+C etc but strangely was fired by CTRL+B.

The workaround fix involved using the keyup event instead of the keydown.

Here's my code :

public void  ShortCut(object sender, KeyEventArgs e, TextBox  box   )
    {
        string s, tmp1, tmp2;
        int selectionIndex;
        switch (e.KeyCode)
        {
            case Keys.V: // paste 
                if (Clipboard.ContainsText())
                {
                    s = Clipboard.GetText(TextDataFormat.Text);
                    selectionIndex = box.SelectionStart;
                    tmp1 = box.Text.Substring(0, selectionIndex);
                    tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
                    box.Text = tmp1 + s + tmp2;
                }

                break;
            case Keys.C: // copy 
                if (box.SelectionLength > 0)
                {
                    selectionIndex = box.SelectionStart;
                    s = box.Text.Substring(selectionIndex, box.SelectionLength);
                    Clipboard.SetText(s);
                }
                break;
            case Keys.X: // cut 
                if (box.SelectionLength > 0)
                {
                    selectionIndex = box.SelectionStart;
                    s = box.Text.Substring(selectionIndex, box.SelectionLength);
                    Clipboard.SetText(s);
                    tmp1 = box.Text.Substring(0, selectionIndex);
                    tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
                    box.Text = tmp1 + tmp2;
                }


                break;
            case Keys.A: // all 
                box.SelectAll();
                break;

        }

    }

//and here's an example calling it :

 private void textBoxExpression_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Control)
        {
            m_Host.ShortCut(sender, e, textBoxExpression);


        }
        else
        {

               ....

        }


}