Catching Ctrl + C in a textbox

后端 未结 8 1724
孤城傲影
孤城傲影 2020-12-15 20:42

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 +

相关标签:
8条回答
  • 2020-12-15 21:11

    Try the following: capture the up arrow and down arrow events. When you detect down arrow for CTRL, set a flag; when you detect up arrow, reset the flag. If you detect the C key while the flag is set, you have Ctrl+C.

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

    0 讨论(0)
  • 2020-12-15 21:11

    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.

    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();
    }
    
    0 讨论(0)
  • 2020-12-15 21:16

    If you want to catch such combinations of keys in KeyPress Event look at this table here:

    http://www.physics.udel.edu/~watson/scen103/ascii.html

    in Non-Printing Characters section you can see the Dec numbers for each combination. For example, Dec number for Ctrl + C is 3. So you can catch it in KeyPress Event like this:

    private void btnTarget_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar != 3) // if it is not Ctrl + C
        {
           // do something
        }
    }
    
    0 讨论(0)
  • 2020-12-15 21:16

    For me, it's not working with KeyDown event so I tried with PreviewKeyDown and it's worked.

    private void txt_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
      if (e.Control == true && e.KeyCode == Keys.C)
      {
        Clipboard.SetText(txt.SelectedText);
      }
    }
    
    0 讨论(0)
  • 2020-12-15 21:18

    I had a problem catching Ctrl + C on a TextBox by KeyDown. I only got Control key when both Control and C were pressed. The solution was using 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();
        }
    }
    
    0 讨论(0)
  • 2020-12-15 21:19

    Key events occur in the following order:

    1. KeyDown
    2. KeyPress
    3. KeyUp

    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.

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

    0 讨论(0)
提交回复
热议问题