What is equivalent to getch () and getche () on Linux?

I cannot find the equivalent header file for conio.h on Linux.

Is there an option for getch() and getche() on Linux?

I want to create a basic menu for the case of switching, where the user will provide their option, simply by pressing a single key, and the process should be advanced. I do not want to allow the user to press the ENTER key after selection.

+53
c linux getch getchar getc
Sep 19 '11 at 9:55
source share
7 answers
 #include <termios.h> #include <stdio.h> static struct termios old, current; /* Initialize new terminal i/o settings */ void initTermios(int echo) { tcgetattr(0, &old); /* grab old terminal i/o settings */ current = old; /* make new settings same as old settings */ current.c_lflag &= ~ICANON; /* disable buffered i/o */ if (echo) { current.c_lflag |= ECHO; /* set echo mode */ } else { current.c_lflag &= ~ECHO; /* set no echo mode */ } tcsetattr(0, TCSANOW, &current); /* use these new terminal i/o settings now */ } /* Restore old terminal i/o settings */ void resetTermios(void) { tcsetattr(0, TCSANOW, &old); } /* Read 1 character - echo defines echo mode */ char getch_(int echo) { char ch; initTermios(echo); ch = getchar(); resetTermios(); return ch; } /* Read 1 character without echo */ char getch(void) { return getch_(0); } /* Read 1 character with echo */ char getche(void) { return getch_(1); } /* Let test it out */ int main(void) { char c; printf("(getche example) please type a letter: "); c = getche(); printf("\nYou typed: %c\n", c); printf("(getch example) please type a letter..."); c = getch(); printf("\nYou typed: %c\n", c); return 0; } 

Exit:

 (getche example) please type a letter: g You typed: g (getch example) please type a letter... You typed: g 
+68
Sep 19 '11 at 10:21
source share
 #include <unistd.h> #include <termios.h> char getch(void) { char buf = 0; struct termios old = {0}; fflush(stdout); if(tcgetattr(0, &old) < 0) perror("tcsetattr()"); old.c_lflag &= ~ICANON; old.c_lflag &= ~ECHO; old.c_cc[VMIN] = 1; old.c_cc[VTIME] = 0; if(tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANON"); if(read(0, &buf, 1) < 0) perror("read()"); old.c_lflag |= ICANON; old.c_lflag |= ECHO; if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON"); printf("%c\n", buf); return buf; } 

Remove the last printf if you do not want the character to be displayed.

+30
May 03 '13 at 14:48
source share

I suggest you use curses.h or ncurses.h, they implement keyboard control procedures, including getch (). You have several options for changing getch behavior (i.e. wait for a keystroke or not).

+7
Sep 19 '11 at 10:03
source share

The ncurses library has a getch () function. You can get it by installing the ncurses-dev package.

+4
Sep 19 '11 at 10:01
source share

You can use the curses.h library on Linux as indicated in another answer.

You can install it in Ubuntu:

Sudo apt-get update

sudo apt-get install ncurses-dev

I took part of the installation from here .

0
Jan 01 '15 at 11:15
source share

As said above, getch() is in the ncurses . ncurses should be initialized, see for example getchar () returns the same value (27) for up and down arrow keys for this

0
Sep 07 '17 at 13:03 on
source share

The replacement for getch() is getchar() declared in stdio.h . getchar() is available on Windows and Linux.

The following is a comment by Max Trucks .

There are some (somewhat important) differences between getch() and getchar() .

1) getch() returns immediately after pressing a key. getchar() allows you to enter indefinitely, until you enter EOL.

2) getch() does not display anything. getchar() records everything that you type on the screen (even EOL).

If these two differences are not important to the user, you can use getchar() as a replacement, otherwise this might not be the best idea.

-2
Mar 26 '14 at 20:16
source share



All Articles