Trying to detect keypress

前端 未结 4 1800
面向向阳花
面向向阳花 2020-12-21 02:29

I made a method that detects when a key is pressed, but its not working! Heres my code

void KeyDetect(object sender, KeyEventArgs e)
{ 
    if (e.KeyCode ==          


        
相关标签:
4条回答
  • 2020-12-21 02:34

    Use keypress event like this:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyCode == Keys.F1 && e.Alt)
        {
            //do something
        }
    }
    
    0 讨论(0)
  • 2020-12-21 02:37

    1) Go to your form's Properties

    2) Look for the "Misc" section and make sure "KeyPreview" is set to "True"

    3) Go to your form's Events

    4) Look for the "Key" section and double click "KeyDown" to generate a function to handle key down events

    Here is some example code:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine("You pressed " + e.KeyCode);
            if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
            {
                //Do Something if the 0 key is pressed (includes Num Pad 0)
            }
        }
    
    0 讨论(0)
  • 2020-12-21 02:37

    You are looking for this.KeyPress. See How to Handle Keypress Events on MSDN.

    0 讨论(0)
  • 2020-12-21 02:50

    Try to use the KeyDown event.

    Just see KeyDown in MSDN

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