Jump to content
  • Advertisement
Sign in to follow this  
NotSlyPee

C# KeyDown problem

This topic is 4138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't get KeyDown method to work. I need my application to check answer when user clicks 'enter' on his keyboard, but unfortunately nothing happens. Here's the code:
 private void Answer_KeyDown(object sender, KeyEventArgs e)
        
        {
            if (e.KeyCode == Keys.Enter)
            {
                int result = 0;
                if (problemType == "+")
                {
                    result = no1 + no2;
                }
                else if (problemType == "-")
                {
                    result = no1 - no2;
                }
                else if (problemType == "*")
                {
                    result = no1 * no2;
                }

                if (Answer.Text == result.ToString())
                {
                    Feedback.Text = "Correct";
                }
                else
                {
                    Feedback.Text = "Incorrect";
                }

Share this post


Link to post
Share on other sites
Advertisement
1. Make sure the control actually gets focus
2. If it's a TextBox or derived, there are "AcceptsReturn" and "AcceptsTab" properties that you need to set to 'true' if you want KeyDown events for those two keys.
3. In rare cases, override the "IsInputKey" method and return true for enter, otherwise return base.IsInputKey.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
1. Make sure the control actually gets focus
2. If it's a TextBox or derived, there are "AcceptsReturn" and "AcceptsTab" properties that you need to set to 'true' if you want KeyDown events for those two keys.
3. In rare cases, override the "IsInputKey" method and return true for enter, otherwise return base.IsInputKey.


I didn't understood the get focus part. Can you tell a bit more about it?

Share this post


Link to post
Share on other sites
Well, if it's a TextBox and you're successfully typing text into it, it has focus. If you can't tell if any input is getting to it at all then it might not.

Focus means that the control is the one selected to receive keyboard events.

Typically Focus is set when you click on a control or press the TAB key to move between controls.

Various controls have their "Selectable" style (see "Control.Setstyle") true or false, which can affect this.

Share this post


Link to post
Share on other sites
Thanks and yes, it has focus and "AcceptsReturn" and "AcceptsTab" properties are set to 'true'. It's still doesn't work.

Share this post


Link to post
Share on other sites
If AcceptsReturn is true and you're not getting the keydown it's possible that the event hasn't been hooked. Your function by itself won't be called unless you added it to the event, which requires something that looks like the following line:

Answer.KeyDown += new KeyEventHandler(Answer_Keydown);

(edit) oops

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!