Coder Perfect

In Linux, what is the equivalent of getch() and getche()?

Problem

In Linux, I’m unable to locate the equivalent header file for conio.h.

Is there a Linux equivalent of the getch() and getche() functions?

I’d like to create a switch case-based menu where the user may select an option by hitting a single key and the process will proceed. I don’t want the user to be able to press ENTER after making his selection.

Asked by Jeegar Patel

Solution #1

#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

Answered by niko

Solution #2

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

If you don’t want the character to be displayed, remove the last printf.

Answered by 3 revs, 2 users 72%

Solution #3

I recommend that you employ curses. h or ncurses are two options. 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).

Answered by Fafaman

Solution #4

The ncurses library has a getch() method. Installing the ncurses-dev package will provide you with it.

Answered by Jan S

Solution #5

You are free to utilize the curses. As noted in the other answer, the h library is available in Linux.

You can install it in Ubuntu by:

This is where I got the installation information.

Answered by Ashish Ahuja

Post is based on https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux