Catching Ctrl + C in a textbox

后端 未结 8 1725
孤城傲影
孤城傲影 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:25

    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:

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

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

    Also, to prevent processing the keystroke by the underlying TextBox, set the SuppressKeyPress property to true as shown.

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