Find the answer to your Linux question:
Results 1 to 7 of 7
Is there a POSIX equivalent of the getch() function? I need it because it doesn't wait for the enter key to be pressed as the ANSI getchar() does....
  1. #1
    Linux User GNU_man's Avatar
    Join Date
    Apr 2005
    Location
    Canada, eh
    Posts
    284

    getch() for GCC

    Is there a POSIX equivalent of the getch() function? I need it because it doesn't wait for the enter key to be pressed as the ANSI getchar() does.
    PTL x10 Hallelujah!
    AMD Athlon XP 2600+ 512MB RAM Dual 80G WD HD 8MB Cache (1 WinXP Home, 1 CentOS 4.2) GeForce Ti4200 128MB SB Live! 5.1
    Registered Linux user #391521

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    man getc
    That should give you a list of all the getchar()-like functions

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    If you don't want to have to wait until Enter is pressed, you simply need to turn off terminal buffering, using the tcsetattr function. There was another thread which discussed that in some depth.

  4. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    I've also had good luck with the readline library when it comes to doing input manipulation, tab-completion, etc. but Dolda's method is much simpler/straightforward.

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575

    Re: getch() for GCC

    Quote Originally Posted by GNU_man
    Is there a POSIX equivalent of the getch() function? I need it because it doesn't wait for the enter key to be pressed as the ANSI getchar() does.
    I'm not sure what POSIX says about getch() but you can modify whether
    it waits or not by using nodelay()

    Or did you mean if there is a POSIX function similar to the getch() of some
    other (ahem) operating system ? Well there is a function called getch() so
    you only need to type man getch and you'll see if it's what you want.

  6. #6
    Just Joined!
    Join Date
    Jul 2006
    Posts
    4

    getch() solution in linux

    /*solution provided by kermi3 from this web posting C Board

    #include <termios.h>
    #include <unistd.h>

    int mygetch(void)
    {
    struct termios oldt,
    newt;
    int ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
    }

    I would save that text above and set it as a header file the function you would call would be mygetch() works just like getch()

  7. #7
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    The curses library has also a getch function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...