6

I've tried to override WndProc, but no message show up on paste event.

Then I tried to create custom filter and using method PreFilterMessage I was able to catch message with value 257 (KEYUP event), but that's not enough...

2 Answers2

18

Use:

 protected override void OnKeyDown(KeyEventArgs e)
 {
      if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
      {
            MessageBox.Show("Hello world");
      }
      base.OnKeyDown(e);
  }

Make sure your form KeyPreview = true.

Goran
  • 6,798
  • 9
  • 41
  • 57
2

You can do this by:

  • Intercepting the Ctrl+V in KeyDown (or KeyUp) of your form
  • Creating a menu in your form that contains a Paste option that has the Ctrl+V shortcut (this would maybe be better since you will have users looking for the options)
  • Intercepting the KEYDOWN message like you described in the question and checking whether Ctrl is pressed at that time (I think this is the hardest of all 3).

Personally I would go for using a menu option.

AEM
  • 1,354
  • 8
  • 20
  • 30
Dan Cristoloveanu
  • 1,974
  • 1
  • 13
  • 20