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

荒凉一梦 提交于 2019-11-26 06:36:08

问题


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


回答1:


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.




回答2:


Check out curses:

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




回答3:


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




回答4:


getch() seems to be included in curses library.




回答5:


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.




回答6:


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




回答7:


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'




回答8:


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




回答9:


You can use getch() equivalent from libcaca:

__extern int caca_conio_getch (void)



回答10:


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


来源:https://stackoverflow.com/questions/3276546/how-to-implement-getch-function-of-c-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!