【发布时间】:2010-07-18 17:40:17
【问题描述】:
在 TurboC++ 中,我可以使用来自 conio.h 的 getch() 函数。但在 Linux 中,gcc 不提供conio.h。如何获得getch()的功能?
【问题讨论】:
-
turbo c 已经死了。不要使用它。
在 TurboC++ 中,我可以使用来自 conio.h 的 getch() 函数。但在 Linux 中,gcc 不提供conio.h。如何获得getch()的功能?
【问题讨论】:
试试这个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;
}
你也可以使用gcc中的ncurses库来实现一些类似于conio.h的功能。
【讨论】:
ICANON 标志吗?它的作用是什么?
如果回显到屏幕没有问题,您可以尝试使用stdio.h 中的getchar()。
【讨论】:
getch() 和getchar() 之间的唯一区别。 getch() 在从缓冲区读取之前不会等待回车。例如。要使用getchar() 输入“a”,您必须输入a[ENTER]。使用getch(),您只需要输入'a'。
【讨论】:
getch() 函数。
conio.h 的一部分,那么您可能正在使用其他部分。如果你正在移植一个大量使用conio.h 的应用程序,那么curses 可能是最好的选择。我认为downvote更像是只看链接的答案。
@987654321@ 似乎包含在curses library 中。
【讨论】:
根据这些解决方案code,您必须手动使用 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);
}
把它放在你的主要代码方法之前
【讨论】:
您可以使用与libcaca 等效的getch():
__extern int caca_conio_getch (void)
【讨论】:
如果由于任何原因你不能使用诅咒,试试这个:
# 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);
}
【讨论】:
在 Unix 中,getch() 是 ncurses 库的一部分。但是我为 this question 写了一个解决方法,它可以让你使用类似 getch 的功能,而无需其他的诅咒包袱。
【讨论】:
conio.h 只在 Dos 中,
对于 linux,使用
sudo apt-get install libncurses-dev
& 然后
-lncurses
// 在 IDE 中,你必须链接它: 例如:代码块,设置 -> 编译器 -> 链接器设置,并添加 'ncurses'
【讨论】:
getch() 在libcurses 中。 curses 的使用有点复杂,因为它与底层终端有很深的链接并且必须被初始化。带有 libcurses 初始化的 curses getch() 的工作示例位于 getchar() returns the same value (27) for up and down arrow keys
【讨论】:
你也可以像这样在linux中使用系统命令来控制终端
char getch() {
char c;
system("stty raw -echo");
c = getchar();
system("stty -raw echo");
return c;
}
此功能不需要用户按回车键,并从用户那里获取输入而不回显它需要您将 stdlib.h 库添加到您的代码中
注意:此功能仅适用于基于 UNIX 的操作系统
我们将不胜感激任何改进或指出代码中的任何问题
问候
【讨论】: