如何在Linux中实现C的getch()函数?

时间:2022-10-16 16:14:44

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

在TurboC++中,我可以使用conio.h中的getch()函数。但是在Linux中,gcc不提供conio.h。如何获得getch()的功能?

9 个解决方案

#1


24  

Try this conio.h file:

试试这个conio。h文件:

#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.

您还可以使用gcc中的ncurses库来实现一些类似于conio.h的函数。

#2


7  

Check out curses:

看看诅咒:

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

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

#3


6  

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

如果回显到屏幕不是问题,您可以尝试使用stdi .h中的getchar()。

#4


1  

getch() seems to be included in curses library.

getch()似乎包含在curses库中。

#5


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.

在Unix中,getch()是ncurses库的一部分。但是我为这个问题编写了一个变通方案,允许您使用类似getch的功能,而不需要其他的咒骂。

#6


0  

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

根据这些解决方案代码,您必须手动使用getch()和getche()函数的开放源代码,代码如下所示。

#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


0  

conio.h is only in Dos,

conio。h只在Dos中,

for linux, use

对于linux,使用

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'

//在IDE中,您必须链接它:例如:codeblocks,设置—>编译器—>链接器设置,并添加“ncurses”

#8


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

在libcurses getch()。使用curses要复杂一些,因为它有到底层终端的深层链接,并且必须进行初始化。一个具有libcurses getch()的工作示例是getchar(),它为上下箭头键返回相同的值(27)

#9


0  

You can use getch() equivalent from libcaca:

您可以使用libcaca中的getch():

__extern int caca_conio_getch (void)

#1


24  

Try this conio.h file:

试试这个conio。h文件:

#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.

您还可以使用gcc中的ncurses库来实现一些类似于conio.h的函数。

#2


7  

Check out curses:

看看诅咒:

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

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

#3


6  

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

如果回显到屏幕不是问题,您可以尝试使用stdi .h中的getchar()。

#4


1  

getch() seems to be included in curses library.

getch()似乎包含在curses库中。

#5


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.

在Unix中,getch()是ncurses库的一部分。但是我为这个问题编写了一个变通方案,允许您使用类似getch的功能,而不需要其他的咒骂。

#6


0  

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

根据这些解决方案代码,您必须手动使用getch()和getche()函数的开放源代码,代码如下所示。

#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


0  

conio.h is only in Dos,

conio。h只在Dos中,

for linux, use

对于linux,使用

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'

//在IDE中,您必须链接它:例如:codeblocks,设置—>编译器—>链接器设置,并添加“ncurses”

#8


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

在libcurses getch()。使用curses要复杂一些,因为它有到底层终端的深层链接,并且必须进行初始化。一个具有libcurses getch()的工作示例是getchar(),它为上下箭头键返回相同的值(27)

#9


0  

You can use getch() equivalent from libcaca:

您可以使用libcaca中的getch():

__extern int caca_conio_getch (void)