Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement getch() function of C in Linux?

Tags:

c

gcc

getch

conio

In TurboC++, I can use the getch() function from conio.h. But in Linux, gcc doesn't provide conio.h. How can I get the functionality of getch()?

like image 842
Hits Avatar asked Jul 18 '10 17:07

Hits


People also ask

How is Getch implemented in Linux?

Echoing to the screen is not the only difference between getch() and getchar() . getch() doesn't wait for a carriage return before being reading from the buffer. E.g. to input 'a' using getchar() , you have to type a[ENTER] . With getch() , you only need type 'a'.

What is the use of getch () in C?

getch() method pauses the Output Console until a key is pressed. It does not use any buffer to store the input character. The entered character is immediately returned without waiting for the enter key.

In which header file is getch () declared?

Because getch is not a standardized function. And as such, not in stdio. h . On some platforms, it's defined in conio.


7 Answers

Try this conio.h file:

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

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

You can also use the ncurses library in gcc for some functions similar to conio.h.

like image 138
Shobhit Avatar answered Oct 07 '22 18:10

Shobhit


If echoing to the screen is not a problem, you could try using getchar() from stdio.h.

like image 39
Carl Smotricz Avatar answered Oct 07 '22 18:10

Carl Smotricz


Check out curses:

http://en.wikipedia.org/wiki/Curses_%28programming_library%29

like image 36
Jamie Wong Avatar answered Oct 07 '22 19:10

Jamie Wong


getch() seems to be included in curses library.

like image 42
che Avatar answered Oct 07 '22 18:10

che


According to these solution code you must manually use open source code for getch() and getche() function as described the code is as following .

#include <termios.h>
#include <stdio.h>

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  new = old; /* make new settings same as old settings */
  new.c_lflag &= ~ICANON; /* disable buffered i/o */
  new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
  tcsetattr(0, TCSANOW, &new); /* 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);
}

Just put it before your main method of code

like image 33
Shubham Sharma Avatar answered Oct 07 '22 19:10

Shubham Sharma


You can use getch() equivalent from libcaca:

__extern int caca_conio_getch (void)
like image 37
Sauron Avatar answered Oct 07 '22 17:10

Sauron


If, for any reasons, you can't use curses, try this:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <termios.h>

/* get a single char from stdin    */
int getch(void)
{
   struct termios oldattr, newattr;
   int ch;
   tcgetattr(0, &oldattr);
   newattr=oldattr;
   newattr.c_lflag &= ~( ICANON | ECHO );
   tcsetattr( 0, TCSANOW, &newattr);
   ch=getchar();
   tcsetattr(0, TCSANOW, &oldattr);
   return(ch);
}
like image 24
piper Avatar answered Oct 07 '22 17:10

piper