Press Escape key to call method

后端 未结 9 1930
南笙
南笙 2020-12-16 14:25

Is there a way to start a method in C# if a key is pressed? For example, Esc?

相关标签:
9条回答
  • 2020-12-16 15:11

    use the OnKeyPress Event of your textbox and in the event

    if(e.KeyCode==Keys.Escape)
    {
        yourTextBox.Text = string.Empty;
    }
    
    0 讨论(0)
  • 2020-12-16 15:13

    First in Properties do > KeyPreview : True

    Then :

    private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                //call your method here
            }
        }
    
    0 讨论(0)
  • 2020-12-16 15:14

    In case someone is looking for how to do this in a console application

    if (Console.ReadKey().Key == ConsoleKey.Escape)
    {
        return;
    }
    
    0 讨论(0)
提交回复
热议问题