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. Which function is like "getch()" in Console App?

Which function is like "getch()" in Console App?

Scheduled Pinned Locked Moved C#
csharpquestion
12 Posts 4 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.
  • L Lane Yu

    Hi, i'm starting to learn C#.:laugh: I want to output a pause(such as "press any key to continue...") in a console application. I just need a function like "getch()" in C language. Which object and method to use? Thanks.

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #2

    Console.Read(); or Console.ReadLine();

    1 Reply Last reply
    0
    • L Lane Yu

      Hi, i'm starting to learn C#.:laugh: I want to output a pause(such as "press any key to continue...") in a console application. I just need a function like "getch()" in C language. Which object and method to use? Thanks.

      F Offline
      F Offline
      Febret
      wrote on last edited by
      #3

      Console.ReadKey should do the trick. Bye!

      J 1 Reply Last reply
      0
      • F Febret

        Console.ReadKey should do the trick. Bye!

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #4

        There is no ReadKey method on System.Console :wtf:

        F 1 Reply Last reply
        0
        • J J4amieC

          There is no ReadKey method on System.Console :wtf:

          F Offline
          F Offline
          Febret
          wrote on last edited by
          #5

          Whoops: Console.ReadKey is on Framework 2.0 only, sorry :doh:

          L 1 Reply Last reply
          0
          • F Febret

            Whoops: Console.ReadKey is on Framework 2.0 only, sorry :doh:

            L Offline
            L Offline
            Lane Yu
            wrote on last edited by
            #6

            You mean i'm helpless unless downloading Framework2.0?:sigh:

            J F 2 Replies Last reply
            0
            • L Lane Yu

              You mean i'm helpless unless downloading Framework2.0?:sigh:

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #7

              No, you're only helplessuntil you go back and read the replies properly :wtf:

              1 Reply Last reply
              0
              • L Lane Yu

                You mean i'm helpless unless downloading Framework2.0?:sigh:

                F Offline
                F Offline
                Febret
                wrote on last edited by
                #8

                Of course not :-D In Framework 1.0 / 1.1 You have to use Console.Read(), as J4amieC suggested. Bye!

                L 1 Reply Last reply
                0
                • F Febret

                  Of course not :-D In Framework 1.0 / 1.1 You have to use Console.Read(), as J4amieC suggested. Bye!

                  L Offline
                  L Offline
                  Lane Yu
                  wrote on last edited by
                  #9

                  But Console.Read() is not as getch(). I only want a single char from keyboard without echo. :confused:

                  R F 2 Replies Last reply
                  0
                  • L Lane Yu

                    But Console.Read() is not as getch(). I only want a single char from keyboard without echo. :confused:

                    R Offline
                    R Offline
                    Rob Philpott
                    wrote on last edited by
                    #10

                    Not sure this is possible using just the Console object prior to .NET 2.0 - it's a very minimalist implementation. Should be straightforward enough using P/Invoke with the Platform SDK though. Take a look at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_reference.asp[^], and drop me a line if you get stuck. Regards, Rob Philpott.

                    L 1 Reply Last reply
                    0
                    • R Rob Philpott

                      Not sure this is possible using just the Console object prior to .NET 2.0 - it's a very minimalist implementation. Should be straightforward enough using P/Invoke with the Platform SDK though. Take a look at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_reference.asp[^], and drop me a line if you get stuck. Regards, Rob Philpott.

                      L Offline
                      L Offline
                      Lane Yu
                      wrote on last edited by
                      #11

                      I see. I'm a begginer of C#. So far I don't know how to call a Windows API. So I think I should learn more firstly. Thanks all of you.:-D

                      1 Reply Last reply
                      0
                      • L Lane Yu

                        But Console.Read() is not as getch(). I only want a single char from keyboard without echo. :confused:

                        F Offline
                        F Offline
                        Febret
                        wrote on last edited by
                        #12

                        I see: if you want to read a single character from the user (any charachter, not an enter key press), and you don't want that character to be shown, the only way to do it on .Net 1.0 /1.1 (the 2.0 Console.ReadKey does the same thing) is to import _getch from msvcrt.dll. You can use this code (by reinux from http://www.codeproject.com/useritems/PressAnyKeyToContinue.asp):

                        [DllImport("msvcrt.dll")]
                        private static extern int _getch();

                        public static int Getch()
                        {
                        try
                        {
                        return _getch();
                        }
                        catch
                        {
                        return Console.Read();
                        }
                        }

                        Insert this into a class and then you should be able to use Getch() to do what you need. Bye!

                        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