简体   繁体   English

只要有一个带焦点的只读文本框,就无法在keydown事件上检测到Ctrl + Key快捷方式

[英]Can't detect a Ctrl + Key shortcut on keydown events whenever there's a readonly textbox with focus

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: 我有一个注册表单中的以下keydown事件处理程序,其中包含几个禁用和只读文本框,它们只是按钮的简单快捷方式:

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. 表单将KeyPreview设置为true,但只要readonly文本框具有焦点并按Ctrl + E,我就无法获得“Control.ModifierKeys == Keys.Control”和“e.KeyCode == Keys.E”两者都为true与此同时。 What is really strange is that Ctrl + W works. 真正奇怪的是Ctrl + W有效。 Anyone has any idea what the hell is going on? 任何人都知道到底是怎么回事? :( :(

According to this question and this one , It looks like a more general way to handle keyboard shortcuts is to override the ProcessCmdKey() method: 根据这个问题,并且这一个 ,它看起来像处理键盘快捷键更普遍的方法是重写给ProcessCmdKey()方法:

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? 您是否考虑过使用Alt + EAlt + S并仅为按钮设置助记符属性? That seems to work well for me, and it's easier to set up. 这似乎对我有用,而且设置起来更容易。

I had the same problem 我有同样的问题

In my application the shortcuts did not work if the form was opened with an invoked show(). 在我的应用程序中,如果使用调用的show()打开表单,则快捷方式不起作用。 They did work if the form was opened with ShowDialog(). 如果使用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. 我还发现文本框的keydown事件没有被CTRL + C等触发,但奇怪的是被CTRL + B触发了。

The workaround fix involved using the keyup event instead of the keydown. 解决方法修复涉及使用keyup事件而不是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
        {

               ....

        }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM