How to capture Ctrl-Z keystroke in RichTextBox

I need to capture when the user clicks CTRL-Z(simultaneously press CTRLand Z) in the RichTextBox control.

I disabled the property ShortCutsEnabled. I tried every combination that I can come up with, using KeyCodeboth KeyDataevents KeyDownand KeyPress.

I can capture A CTRLOR a Z, but never both together. Is it a RichTextBoxcapture of this keystroke before I can see it, even if the shortcuts are disabled?

Does anyone have a solution that works for this?

+4
source share
3 answers

CTRL-Z

 textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);

void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
         if(e.KeyCode == Keys.Z && (e.Control)) {
             MessageBox.Show("Ctrl + Z Pressed!");
         }
    }
+3

KeyCode Modifiers KeyDown:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z)
        MessageBox.Show("Ctrl-Z Pressed");
}
+1
  void richTextBox1_KeyDown(object sender, KeyEventArgs e)
  {
      if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.Z)
        {
             MessageBox.Show("Ctrl + Z is Pressed");
        }
  }

.

0
source

Source: https://habr.com/ru/post/1536710/


All Articles