如何在 C# 应用程序中捕获 Control-V?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/173593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch Control-V in C# app?
提问by
I've tried to override WndProc, but no message show up on paste event.
我试图覆盖 WndProc,但没有消息显示在粘贴事件上。
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...
然后我尝试创建自定义过滤器并使用方法 PreFilterMessage 我能够捕获值为 257(KEYUP 事件)的消息,但这还不够......
回答by Goran
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.
确保您的表单 KeyPreview = true。
回答by Dan Cristoloveanu
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).
- 拦截窗体的 KeyDown(或 KeyUp)中的 Ctrl+V
- 在您的表单中创建一个菜单,其中包含具有 Ctrl+V 快捷方式的粘贴选项(这可能会更好,因为您将让用户寻找选项)
- 像您在问题中描述的那样拦截 KEYDOWN 消息并检查当时是否按下了 Ctrl (我认为这是所有 3 个中最难的)。
Personally I would go for using a menu option.
我个人会去使用菜单选项。