Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to handle Enter key...

How to handle Enter key...

Scheduled Pinned Locked Moved C#
databasetutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mukundkallapur
    wrote on last edited by
    #1

    Please go through...

    class Test
    {

    Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
    Console.WriteLine();
    Console.WriteLine("Press 9 to Exit or Enter to Continue ");

    string s=Console.ReadLine();

    ....

    }

    My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution

    OriginalGriffO A 2 Replies Last reply
    0
    • M mukundkallapur

      Please go through...

      class Test
      {

      Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
      Console.WriteLine();
      Console.WriteLine("Press 9 to Exit or Enter to Continue ");

      string s=Console.ReadLine();

      ....

      }

      My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Yes it will. I assume you have something along the lines of:

      do
      {
      Console.WriteLine(...
      ...
      s = Console.ReadLine();
      } while (s != "9")

      If so, then it will loop round for any sequence of keypresses follewed by an ENTER other than the '9' key followed by ENTER. Remember that ReadLine will retrun with what has been pressed up to an ENTER - so if you only press enter, you will get and empty string. You will have to check for this and act accordingly. Sorry if this sound vague, but your code fragment doesn't give us a lot to go on!

      All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M mukundkallapur

        Please go through...

        class Test
        {

        Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
        Console.WriteLine();
        Console.WriteLine("Press 9 to Exit or Enter to Continue ");

        string s=Console.ReadLine();

        ....

        }

        My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution

        A Offline
        A Offline
        Alex Manolescu
        wrote on last edited by
        #3

        hi, for reading a key you can use ReadKey method and a good practice for you're application is to use a switch statement like this:

                Console.WriteLine("1:Add\\n2:Sub\\n3:Mult:\\n4:Div");
                Console.WriteLine();
                Console.WriteLine("Press 9 to Exit or Enter to Continue ");
               
                ConsoleKeyInfo cki = Console.ReadKey();
        
                switch (cki)
                {
                    case ConsoleKey.D9:
                        Console.WriteLine("exit");
                        break;
        
                    case ConsoleKey.Enter:
                        Console.WriteLine("continue");
                        break;
        
                    case ConsoleKey.D1:
                        Console.WriteLine("Add");
                        break;
        
                    case ConsoleKey.D2:
                        Console.WriteLine("Sub");
                        break;
        
                    case ConsoleKey.D3:
                        Console.WriteLine("Mult");
                        break;
        
                    case ConsoleKey.D4:
                        Console.WriteLine("Div");
                        break;
        
                    default: break;
                }
        

        Anyway I don't understand why use the ENTER key to continue? have you more option than 1,2,3 and 4 ? :) Cheer's, Alex Manolescu

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups