简体   繁体   中英

C#: KeyDown Event does not work in controls

I am trying to finish my final school project. I am creating a c# winform game to be specific. We can not use anything else. I will not be posting here code because I got it pretty messed up and I guess u can answer me just with this info.

Setup:

I got my program set up like this. The is main form and two user controls. I switch those controls within the main form during the game. The first one is MENU and the second one is GUI with picturebox acting as a gamescene .

Problem:

Setup quite not important I guess. But what I need to do is to do some action when I press key Down on the first Control ( while it is active in the form). Sounds easy I thought at first but the onKeyDown event in the menu.cs (1st usercontrol) is doing nothing when i press the key(The event method is not blank). I tried this.previewKey = true; in the menu load method but it did not even recognize it. So my question is: Is there any way to use onKeyDown in usercontrols code?

I did it this way becouse I use the same keys in the second controls and i didnt want it to get messy (which obviously did the oposite huh )

TLDR: Need to use onKeyDown event in userControls (keyPreview might be the key)

BONUS
I also need to somehow link variables from Controls 1 to Form and Controls 2. I looked it up and found out it would be easy in situacion like "Form to Form" but since it is userControls I cant figure it out and I feel like I am just a tiny bit from finishing it.(feels terrible sitting here 9 hours xD please help)

On the keypress event make sure it is for the selected control an not the main form. If you are capturing for the form to determine which key was pressed then use the keypress event for that. You can use a messagebox to verify that you have the right control. Every key has an integer value and you can access and use those by using the properties of e.

Bonus. Depending on how you implemented your code you will have to use either global varibles to pass the data across the forms or use delegates to actively access and set controls on another form

You have to register the event in the Form.Designer.cs :

private void InitializeComponent()
{          
    // Your form properties here

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}

You're KeyDown event can be used like this in the Form.cs code :

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
     // To know if your event is working and the value of the key who's pressed
     MessageBox.Show("Key Pressed = "
                   + e.KeyCode.ToString()
                   + ", Value = "
                   + e.KeyValue.ToString());

     // Example - add some actions bellow
     if (e.KeyValue == 13)
         MessageBox.Show("Return Key Pressed");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM