-2

I am very new to the programming world and recently dove into c#. I don't want to waste your time so I'll get right to it. I wanted to create a program just to test my knowledge, and thought I could attempt to execute specific blocks of code based on which key on the keyboard is pressed by the user. I tried doing this by creating an event handler that contained if statements, but then realized I didn't know how to have the event handler active in the program.

For example, and as you can see in the below snippet, after the WriteLine in Line 5 lets say I wanted to raise the EventKeyPress event so that it waits for user input and reads the key they have pressed and reacts accordingly, how would I do that?

Again, I'm almost a complete beginner and have searched around for explanations about event handlers for hours and still can't wrap my head around what I am supposed to do or if I am even using the event handler correctly. Thanks in advance!

    static void Main(); 
    {
        if (search == "Ball")
        {
            Console.WriteLine("Press enter to exit or backspace to return to the search bar")
            // RIGHT HERE
        }

        else
        {
            Console.WriteLine("Sorry, I don't recognize {0}", search);
        }



        void EventKeyPress(object sender, KeyPressEventArgs e)
        {
            for (int i = 0; i < 1;)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    // exit app
                }

                else if (e.KeyChar == (char)Keys.Back)
                {
                    // go back to search
                }

                else
                {
                    i = 0; // error
                }
            }
        }
    }
3
  • 2
    If this is a console application, it's not based around an eventing concept. Pressing keys does not raise an event - you need to use Console.ReadLine to solicit keyboard input from the user.
    – BJ Myers
    Oct 6, 2018 at 19:37
  • Is this a windows app? A web app? Console? WPF? Oct 6, 2018 at 19:46
  • @BJMyers ohh, I was under the impression that there were other ways to solicit keyboard input from the user, thanks for the clarification Oct 6, 2018 at 20:03

1 Answer 1

1

So, you're asking for something that involves Threading which is not a beginner thing to accomplish at all. The best way to do this for a beginner is to ask for a prompt, then accept as an input. For example.

Console.WriteLine("Hello, what's your name?");
string nameStr = Console.ReadLine();

Console.WriteLine($"Hello, {nameStr}");

You can then use your variable and apply it to an if/while or whatever kind of conditional.

if (nameStr == "Matt"){
//Do This Code.

}

Once you have that code, add a sequential method that will ask the user to return to the main menu or whatever you want it to do.

Main.ReturnMenu(); //Or whatever you want to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.