What is the equivalent to getch() & getche() in Linux?

CLinuxGetcharGetchGetc

C Problem Overview


I am not able to find the equivalent header file for conio.h in Linux.

Is there any option for getch() & getche() function in Linux?

I want to make a switch case base menu where the user will give his option just by pressing one key & process should be moved ahead. I don't want to let user to press ENTER after pressing his choice.

C Solutions


Solution 1 - C

#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's 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;
}

Output:

(getche example) please type a letter: g
You typed: g
(getch example) please type a letter...
You typed: g

Solution 2 - C

#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 don't want the character to be displayed.

Solution 3 - C

I suggest you use curses.h or ncurses.h these implement keyboard management routines including getch(). You have several options to change the behavior of getch (i.e. wait for keypress or not).

Solution 4 - C

There is a getch() function in the ncurses library. You can get it by installing the ncurses-dev package.

Solution 5 - C

You can use the curses.h library in linux as mentioned in the other answer.

You can install it in Ubuntu by:

> sudo apt-get update > > sudo apt-get install ncurses-dev

I took the installation part from here.

Solution 6 - C

As said above getch() is in the ncurses library. ncurses has to be initialized, see i.e. https://stackoverflow.com/questions/15306463/getchar-returns-the-same-value-27-for-up-and-down-arrow-keys for this

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeegar PatelView Question on Stackoverflow
Solution 1 - CnikoView Answer on Stackoverflow
Solution 2 - Cmf_View Answer on Stackoverflow
Solution 3 - CFafamanView Answer on Stackoverflow
Solution 4 - CJan SView Answer on Stackoverflow
Solution 5 - CAshish AhujaView Answer on Stackoverflow
Solution 6 - Cralf htpView Answer on Stackoverflow