在文本框中捕获Ctrl + C [英] Catching Ctrl + C in a textbox
问题描述
尽管我使用C#( Windows窗体)多年,但我的头脑却很聪明失败的时刻,我无法一生,无法弄清楚如何捕获用户在文本框中键入 Ctrl + C 的用户。
Despite me working with C# (Windows Forms) for years, I'm having a brain fail moment, and can't for the life of me figure out how to catch a user typing Ctrl + C into a textbox.
我的应用程序基本上是终端应用程序,我希望 Ctrl + C 发送(byte)3
到串行端口,而不是复制到剪贴板的快捷方式。
My application is basically a terminal application, and I want Ctrl + C to send a (byte)3
to a serial port, rather than be the shortcut for Copy to Clipboard.
我已将快捷方式设置为已启用
属性在文本框上为false。然而,当用户按下 Ctrl + C 时,按键事件不会触发。
I've set the shortcuts enabled
property to false on the textbox. Yet when the user hits Ctrl + C, the keypress event doesn't fire.
如果我按下了keydown ,则当用户按下 Ctrl (即在他们按下 C 键之前)时触发该事件。
If I catch keydown, the event fires when the user presses Ctrl (that is, before they hit the C key).
推荐答案
继续并使用KeyDown事件,但是在该事件中检查是否存在两者 Ctrl 和 C ,如下所示:
Go ahead and use the KeyDown event, but in that event check for both Ctrl and C, like so:
if (e.Control && e.KeyCode == Keys.C) {
//...
e.SuppressKeyPress = true;
}
此外,要防止基础TextBox处理按键,请设置SuppressKeyPress属性如图所示为真。
Also, to prevent processing the keystroke by the underlying TextBox, set the SuppressKeyPress property to true as shown.
这篇关于在文本框中捕获Ctrl + C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!