How to implement getch () function for C on Linux?

In TurboC ++, I can use the getch() function from conio.h . But on Linux, gcc does not provide conio.h . How can I get getch() functionality?

+16
c gcc getch conio
Jul 18 '10 at 17:40
source share
11 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 .

+27
Apr 12 '14 at 19:24
source share
+7
Jul 18 '10 at 17:43
source share

If screen echo is not a problem, you can try using getchar() from stdio.h .

+6
Jul 18 '10 at 17:44
source share

getch() seems to be included in the curses library .

+1
Jul 18 '10 at 17:44
source share

On Unix, getch() is part of the ncurses library. But I wrote a workaround for this question , which allows you to use getch-like functions without the rest of the baggage of damnations.

0
May 22 '12 at 19:00
source share

According to this code solution, you must manually use the open source code for the getch () and getche () functions, as described in the following code.

 #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 the main code method

0
Aug 21 '16 at 11:38 on
source share

conio.h is located only in Dos,

for linux use

 sudo apt-get install libncurses-dev 

& then

 -lncurses 

// In the IDE you need to bind it: for example: codeblocks, Setting → Compiler → Linker setting and add ncurses

0
Oct 22 '16 at 21:21
source share

getch() is in libcurses . using curses is a bit more complicated because it has deep links to the base terminal and needs to be initialized. a working example for curses getch() with libcurses initialization is in getchar () returns the same value (27) for up and down arrow keys

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

You can use the equivalent of getch() from libcaca :

 __extern int caca_conio_getch (void) 
0
Feb 20 '18 at 4:33
source share

If for some reason you cannot 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); } 
0
Jun 20 '18 at 0:24
source share

The getch () function is included in the conio.h library, but the standard conio.h library is not available on linux, we can use the curses.h header file instead. curses.h contains definitions for screen processing and optimization functions.

curses.h documentation

http://pubs.opengroup.org/onlinepubs/007908775/xcurses/curses.h.html

1. Install curses.h on Linux

// For Linux distributions based on Debian

sudo apt install -y libncurses5-dev libncursesw5-dev

// For Fedora

sudo yum install ncurses-devel

2. Compilation program (my .c file is called getchDemo)

g ++ -o outputFile getchDemo.c -lncurses

3. Run the program

./ output file

sample program with output

0
Jan 29 '19 at 9:06
source share



All Articles