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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3276546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:55:53  来源:igfitidea点击:

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

cgccgetchconio

提问by Hits

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 ++,我可以使用getch()从功能conio.h。但是在 Linux 中,gcc 不提供conio.h. 我怎样才能获得 的功能getch()

回答by Shobhit

Try this conio.hfile:

试试这个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 ncurseslibrary in gcc for some functions similar to conio.h.

您还可以将gcc 中的ncurses库用于某些类似于conio.h.

回答by Carl Smotricz

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

如果回显到屏幕没有问题,您可以尝试使用getchar()from stdio.h

回答by che

getch()seems to be included in curses library.

getch()似乎包含在curses库中

回答by Shubham Sharma

According to these solution codeyou 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

把它放在你的主要代码方法之前

回答by piper

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

回答by THEOS

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'

回答by ralf htp

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

getch()libcurses. curses 的使用有点复杂,因为它与底层终端有深层链接并且必须被初始化。getch()带有 libcurses 初始化的curses 的一个工作示例在getchar() 中为向上和向下箭头键返回相同的值(27)

回答by Sauron

You can use getch()equivalent from libcaca:

您可以使用libcaca 中的getch()等效

__extern int caca_conio_getch (void)

回答by luser droog

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

在 Unix 中,getch()是 ncurses 库的一部分。但是我为这个问题写了一个解决方法,让您可以使用类似 getch 的功能,而没有其他的诅咒包袱。