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....
- 08-04-2005 #1
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
- 08-04-2005 #2That should give you a list of all the getchar()-like functionsCode:
man getc
- 08-04-2005 #3Linux 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.
- 08-04-2005 #4
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.
- 08-04-2005 #5Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
Re: getch() for GCC
I'm not sure what POSIX says about getch() but you can modify whether
Originally Posted by GNU_man
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.
- 10-01-2008 #6Just 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()
- 10-01-2008 #7Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The curses library has also a getch function.



