[英]Catching Ctrl + C in a textbox
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. 尽管我多年来一直使用C#( Windows Forms ),但我有一个大脑失败的时刻,并且在我的生活中无法弄清楚如何捕获用户在文本框中键入Ctrl + C。
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. 我的应用程序基本上是一个终端应用程序,我希望Ctrl + C将
(byte)3
发送到串行端口,而不是复制到剪贴板的快捷方式。
I've set the shortcuts enabled
property to false on the textbox. 我在文本框上将快捷方式
enabled
属性设置为false。 Yet when the user hits Ctrl + C , the keypress event doesn't fire. 然而,当用户按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键之前),事件将触发。
It's probably something stupidly simple that I'm missing. 我很遗憾,这可能是一件非常简单的事情。
Go ahead and use the KeyDown event, but in that event check for both Ctrl and C , like so: 继续使用KeyDown事件,但在两个 Ctrl键和C,等,使得事件的检查:
if (e.Control && e.KeyCode == Keys.C) {
//...
e.SuppressKeyPress = true;
}
Also, to prevent processing the keystroke by the underlying TextBox, set the SuppressKeyPress property to true as shown. 另外,为了防止底层TextBox处理击键,请将SuppressKeyPress属性设置为true,如图所示。
I had a problem catching Ctrl + C on a TextBox
by KeyDown
. 我在使用
KeyDown
在TextBox
上捕获Ctrl + C时遇到问题。 I only got Control
key when both Control
and C
were pressed. 当按下
Control
和C
时,我只有Control
键。 The solution was using PreviewKeyDown
: 解决方案是使用
PreviewKeyDown
:
private void OnLoad()
{
textBox.PreviewKeyDown += OnPreviewKeyDown;
textBox.KeyDown += OnKeyDown;
}
private void OnPreviewKeyDown( object sender, PreviewKeyDownEventArgs e)
{
if (e.Control)
{
e.IsInputKey = true;
}
}
private void OnKeyDown( object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C) {
textBox.Copy();
}
}
Key events occur in the following order: 关键事件按以下顺序发生:
The KeyPress event is not raised by noncharacter keys; 非字符键不会引发KeyPress事件; however, the noncharacter keys do raise the KeyDown and KeyUp events.
但是,非字符键确实引发了KeyDown和KeyUp事件。 Control is a noncharacter key.
控制是一个非字符键。
You can check with this line of code: if (e.KeyData == (Keys.Control | Keys.C))
你可以检查这行代码:
if (e.KeyData == (Keys.Control | Keys.C))
D'oh! D'哦! Just figured it out.
刚想通了。 Out of the three possible events, the one I haven't tried is the one I needed!
在三个可能的事件中,我没有尝试过的是我需要的那个! The KeyUp event is the important one:
KeyUp事件是重要的事件:
private void txtConsole_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.C | Keys.Control))
{
_consolePort.Write(new byte[] { 3 }, 0, 1);
e.Handled = true;
}
}
Try the following: capture the up arrow and down arrow events. 请尝试以下操作:捕获向上箭头和向下箭头事件。 When you detect down arrow for CTRL, set a flag;
当您检测到CTRL的向下箭头时 ,设置一个标志; when you detect up arrow , reset the flag.
当您检测到向上箭头时 ,重置标志。 If you detect the C key while the flag is set, you have Ctrl + C .
如果在设置标志时检测到C键, 则按Ctrl + C.
Edit . 编辑 。 Ouch... Jay's answer is definitely better.
哎哟......周杰伦的回答肯定更好。 :-)
:-)
I don't know if it's because some change in newer version or because I am trying to use this on ListBox, but there is no e.Control
in KeyEventArgs e
that I get from KeyDown. 我不知道是不是因为新版本有些变化,或者因为我试图在ListBox上使用它,但是我从KeyDown获得的
KeyEventArgs e
没有e.Control
。
I had to work around solution, I came up with this (it's not the prettiest one, but it works fine): 我不得不解决这个问题,我想出了这个(它不是最漂亮的,但它运行正常):
private List<Key> KeyBuff = new List<Key>();
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (!KeyBuff.Exists(k => k == e.Key))
KeyBuff.Add(e.Key);
if (KeyBuff.Exists(k => k == Key.LeftCtrl || k == Key.RightCtrl) &&
KeyBuff.Exists(k => k == Key.C))
{
// Desired detection
Clipboard.SetText(SelectedText);
}
}
private void ListBox_KeyUp(object sender, KeyEventArgs e)
{
KeyBuff.Clear();
}
If you want to catch such combinations of keys in KeyPress Event
look at this table here: 如果你想在
KeyPress Event
捕获这样的键组合,请在此处查看此表:
http://www.physics.udel.edu/~watson/scen103/ascii.html
http://www.physics.udel.edu/~watson/scen103/ascii.html
in Non-Printing Characters
section you can see the Dec numbers for each combination. 在“
Non-Printing Characters
部分中,您可以看到每个组合的Dec编号。 For example, Dec number for Ctrl + C is 3
. 例如, Ctrl + C的 Dec编号为
3
。 So you can catch it in KeyPress Event like this: 所以你可以在KeyPress事件中捕获它,如下所示:
private void btnTarget_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 3) // if it is not Ctrl + C
{
// do something
}
}
For me, it's not working with KeyDown event so I tried with PreviewKeyDown and it's worked. 对我来说,它不能使用KeyDown事件,所以我尝试使用PreviewKeyDown并且它有效。
private void txt_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.C)
{
Clipboard.SetText(txt.SelectedText);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.