26

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()?

MD XF
  • 7,860
  • 7
  • 40
  • 71
Hits
  • 261
  • 1
  • 3
  • 3

12 Answers12

38

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.

Shobhit
  • 381
  • 3
  • 3
7

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

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • 9
    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'. – Jamie Wong Jul 18 '10 at 19:36
6

Check out curses:

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

Jamie Wong
  • 18,104
  • 8
  • 63
  • 81
  • Agreed, the downvote is unjustified - curses actually provides a `getch()` function. – caf Jul 19 '10 at 00:27
  • 2
    Maybe people see that as an overkill – Isaac Oct 15 '15 at 15:40
  • 3
    @Isaac If you're using one part of `conio.h` then you're probably using others. And of you're porting an application that uses `conio.h` heavily, curses is probably the best choice. I think the downvote is more along the lines of the answer looking link-only. – Damian Yerrick Nov 18 '15 at 20:40
  • 10
    I downvoted this because it's a link only answer, and, in its current form, it should probably rather be a comment than an answer. While this may actually be a relevant link, I'd upvote, if you'd actually add a paragraph of text directly to your post, about the curses library and why you think it is relevant to solve the OPs problem. – moooeeeep Dec 26 '15 at 20:16
1

getch() seems to be included in curses library.

che
  • 12,097
  • 7
  • 42
  • 71
1

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

Community
  • 1
  • 1
Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46
1

You can use getch() equivalent from libcaca:

__extern int caca_conio_getch (void)
Sauron
  • 404
  • 3
  • 14
1

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);
}
piper
  • 31
  • 1
0

In Unix, getch() is part of the ncurses library. But I wrote a workaround for this question that lets you use getch-like functionality without the rest of the curses baggage.

Community
  • 1
  • 1
luser droog
  • 18,988
  • 3
  • 53
  • 105
0

conio.h is only in Dos,

for linux, use

sudo apt-get install libncurses-dev

& then

-lncurses

// In IDE, you have to link it: for example: codeblocks, Setting -> Compiler -> Linker setting, and add 'ncurses'

THEOS
  • 59
  • 5
0

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

ralf htp
  • 9,149
  • 4
  • 22
  • 34
0

You can also use system command to control the terminal in linux like this

char getch()    {

        char c;

        system("stty raw -echo");

        c = getchar();

        system("stty -raw echo");

        return c;

}

This function does not requires the user to press enter and takes input from the user without echoing It requires you to add stdlib.h library to your code

Note : This function is only applicable to UNIX-based OS

Any improvements or pointing out any issues in the code will be appreciated

Regards

  • 1
    Although technically it works, the "system" and dependency on a utility is just an affront to my senses; see https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html – Konrads Apr 30 '21 at 01:47
0

If you want to use conio.h on Ubuntu, then follow these steps:-

  1. Open terminal
  2. sudo apt-get install git
  3. git clone https://github.com/zoelabbb/conio.h.git
  4. cd conio.h
  5. sudo mv conio.h /usr/include/
  6. Close your IDE and open again. Done.

If you are in trouble then follow this link: enter link description here