Thread: What can I use instead of getch() in C#?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    What can I use instead of getch() in C#?

    For gettin input without the user hitting enter in a console application.

    Thanks in advance.
    Why drink and drive when you can smoke and fly?

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Console.Read() or Console.ReadLine()
    To code is divine

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Then how do I do it so they dont wait for the user to press enter?
    Why drink and drive when you can smoke and fly?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    How about _getch()?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
    
    
        class Program
        {
            [DllImport("msvcrt")]
            static extern int _getch();
    
            static void Main(string[] args)
            {
                Console.WriteLine("Hello world");
                Console.WriteLine("Press any key to end..");
                _getch();
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks, Im going to try that.

    [edit]
    Yep, that did it. Thanks a lot.
    Last edited by Marcos; 01-08-2006 at 09:17 PM.
    Why drink and drive when you can smoke and fly?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    Quote Originally Posted by Marcos
    Thanks, Im going to try that.

    [edit]
    Yep, that did it. Thanks a lot.
    Not a problem.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    1
    I think it will be better to use Console.ReadKey(true) insteed of
    [DllImport("msvcrt")]
    static extern int _getch();

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Yeah Console.Readkey(true/false) works best.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    AHHH! Zombie Thread!
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Actually I think this one is quite desirable. He did provide new and meaningful information. ReadKey() is indeed the best option. PInvoke is a last resort solution and rarely advisable. The thread just got richer with his post.

    Could still be closed at this point though, I guess. But for creating an account just to correct a thread you probably got from a search engine... my hat to you, Kirow. And thanks.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    While the info was useful this thread still is considered an old thread and therefore I am going to close it. If anyone would like to discuss the new information feel free to start a new thread about the topic.

    Closed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM