想给RichTextBox添加一个加快捷键试验了
private void RichTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Interaction.MsgBox("2");
if (e.KeyCode == Keys.A) {
e.Handled = true;
e.SuppressKeyPress = false;
Interaction.MsgBox("OK e.KeyCode");
}
}
private void 测试_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Interaction.MsgBox("OK测试_KeyDown");
e.SuppressKeyPress = true; Interaction.MsgBox("1");
}
private void Button2_Click(System.Object sender, System.EventArgs e)
{
this.IsInputKey(Keys.A);
// = True
this.KeyPreview = true;
}private void RichTextBox1_TextChanged(object sender, System.EventArgs e)
{
Interaction.MsgBox("3");
}
是可以触发事件但 RichTextBox1 内容发生了改变,而且RichTextBox1_TextChanged事件最早。
在不用隐藏菜单、HOOK的情况下如果实现快捷键。

解决方案 »

  1.   

    //在form的designer.cs里面重写protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
            {
              if (keyData == (Keys.Ctrl | Keys.I))
              {
                 //你的事件
                }
            } 
      

  2.   


    private void txtData_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.A)
            MessageBox.Show("Ctrl+A");
        if (e.Alt && e.KeyCode == Keys.A)
            MessageBox.Show("Alt+A");
        if (e.Shift && e.KeyCode == Keys.A)
            MessageBox.Show("Shift+A");
    }keydown里面抓KeyEventArgs就可以了
      

  3.   

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    MessageBox.Show("copy?");
                }
                if (e.Control && e.KeyCode == Keys.V)
                {
                    MessageBox.Show("paste?");
                }                
            }
    protected override void WndProc(ref System.Windows.Forms.Message m) 

       base.WndProc(ref m); 
       if(m.Msg == 0x0300 || m.Msg == 0x0301) 
       { 
          
       }