How to catch Control-V in C# app?
16,957
Solution 1
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.
Solution 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.
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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...
-
Goran over 15 yearsFYI - Thats not me asking the question
-
Goran over 15 yearsFirst you'd need to implement such feature (using context menu strip i guess). You'd have full control over it anyway