C# 在文本框中捕获 Ctrl + C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1650648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:41:58  来源:igfitidea点击:

Catching Ctrl + C in a textbox

c#winformstextbox

提问by Bryan

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+ Cinto a textbox.

尽管我使用 C#(Windows 窗体)工作了多年,但我有一个脑力衰竭的时刻,而且我一生都无法弄清楚如何抓住用户在文本框中键入Ctrl+C的方法。

My application is basically a terminal application, and I want Ctrl+ Cto send a (byte)3to a serial port, rather than be the shortcut for Copy to Clipboard.

我的应用程序基本上是一个终端应用程序,我希望Ctrl+C将 a 发送(byte)3到串行端口,而不是复制到剪贴板的快捷方式。

I've set the shortcuts enabledproperty to false on the textbox. Yet when the user hits Ctrl+ C, the keypress event doesn't fire.

我已enabled在文本框中将快捷方式属性设置为 false。然而,当用户点击Ctrl+ 时C,不会触发 keypress 事件。

If I catch keydown, the event fires when the user presses Ctrl(that is, before they hit the Ckey).

如果我捕获 keydown,则在用户按下时Ctrl(即在他们按下C键之前)会触发该事件。

It's probably something stupidly simple that I'm missing.

这可能是我遗漏的一些非常简单的东西。

采纳答案by Jay Riggs

Go ahead and use the KeyDown event, but in that event check for bothCtrland C, like so:

继续使用KeyDown事件,但该事件检查CtrlC,就像这样:

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,如图所示。

回答by Konamiman

Try the following: capture the up arrowand down arrowevents. When you detect down arrowfor CTRL, set a flag; when you detect up arrow, reset the flag. If you detect the Ckey while the flag is set, you have Ctrl+C.

尝试以下操作:捕获up arrowdown arrow事件。当您检测到down arrowCTRL 时,设置一个标志;当您检测到时up arrow,重置标志。如果您在C设置标志时检测到密钥,则您有Ctrl+ C

Edit. Ouch... Jay's answer is definitely better. :-)

编辑。哎哟……杰的回答肯定更好。:-)

回答by ZokiManas

Key events occur in the following order:

关键事件按以下顺序发生:

  1. KeyDown
  2. KeyPress
  3. KeyUp
  1. 按键按下
  2. 按键
  3. 键升

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events. Control is a noncharacter key.

KeyPress 事件不是由非字符键引发的;但是,非字符键确实会引发 KeyDown 和 KeyUp 事件。Control 是一个非字符键。

You can check with this line of code: if (e.KeyData == (Keys.Control | Keys.C))

您可以使用以下代码行进行检查: if (e.KeyData == (Keys.Control | Keys.C))

回答by Bryan

D'oh! 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;
    }
}

回答by Hacko

I had a problem catching Ctrl+ Con a TextBoxby KeyDown. I only got Controlkey when both Controland Cwere pressed. The solution was using PreviewKeyDown:

我在捕获Ctrl+C上时遇到了问题TextBoxby KeyDown。我只有在按下和按下时才拿到Control钥匙。解决方案是使用 :ControlCPreviewKeyDown

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();
    }
}

回答by L. Zeda

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.Controlin KeyEventArgs ethat I get from KeyDown.

我不知道这是否是因为在新版本或者是因为某些变化我想使用这个列表框上,但没有e.ControlKeyEventArgs e,我从得到的KeyDown。

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();
}

回答by Anoopkumar

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);
  }
}

回答by Inside Man

If you want to catch such combinations of keys in KeyPress Eventlook 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 Characterssection you can see the Dec numbers for each combination. For example, Dec number for Ctrl+ Cis 3. So you can catch it in KeyPress Event like this:

Non-Printing Characters部分中,您可以看到每个组合的 Dec 数字。例如,Ctrl+ 的十进制数C3。因此,您可以像这样在 KeyPress 事件中捕获它:

private void btnTarget_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar != 3) // if it is not Ctrl + C
    {
       // do something
    }
}