How to catch Control-V in C# app?
Asked Answered
B

2

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...

Browbeat answered 6/10, 2008 at 8:42 Comment(0)
B
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.

Browbeat answered 6/10, 2008 at 8:50 Comment(2)
FYI - Thats not me asking the questionBrowbeat
First you'd need to implement such feature (using context menu strip i guess). You'd have full control over it anywayBrowbeat
M
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.

Minnie answered 6/10, 2008 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.