Trying to detect keypress

前端 未结 4 1801
面向向阳花
面向向阳花 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: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)
            }
        }
    

提交回复
热议问题