Linux 中 getch() 和 getche() 的等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7469139/
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-08-05 06:15:28  来源:igfitidea点击:

What is the equivalent to getch() & getche() in Linux?

clinuxgetchargetchgetc

提问by Jeegar Patel

I am not able to find the equivalent header file for conio.h in Linux.

我在 Linux 中找不到 conio.h 的等效头文件。

Is there any option for getch()& getche()function in Linux?

Linux 中是否有getch()&getche()功能的选项?

I want to make a switch case base menu where the user will give his option just by pressing one key & process should be moved ahead. I don't want to let user to press ENTER after pressing his choice.

我想制作一个 switch case base menu,用户只需按一个键就可以给出他的选项,进程应该向前移动。我不想让用户在按下他的选择后按下 ENTER。

采纳答案by niko

#include <termios.h>
#include <stdio.h>

static struct termios old, current;

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  current = old; /* make new settings same as old settings */
  current.c_lflag &= ~ICANON; /* disable buffered i/o */
  if (echo) {
      current.c_lflag |= ECHO; /* set echo mode */
  } else {
      current.c_lflag &= ~ECHO; /* set no echo mode */
  }
  tcsetattr(0, TCSANOW, &current); /* 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);
}

/* Let's test it out */
int main(void) {
  char c;
  printf("(getche example) please type a letter: ");
  c = getche();
  printf("\nYou typed: %c\n", c);
  printf("(getch example) please type a letter...");
  c = getch();
  printf("\nYou typed: %c\n", c);
  return 0;
}

Output:

输出:

(getche example) please type a letter: g
You typed: g
(getch example) please type a letter...
You typed: g

回答by Jan S

There is a getch() function in the ncurses library. You can get it by installing the ncurses-dev package.

ncurses 库中有一个 getch() 函数。您可以通过安装 ncurses-dev 包来获取它。

回答by Fafaman

I suggest you use curses.h or ncurses.h these implement keyboard management routines including getch(). You have several options to change the behavior of getch (i.e. wait for keypress or not).

我建议你使用curses.h 或ncurses.h 这些实现键盘管理例程,包括getch()。您有几个选项可以更改 getch 的行为(即是否等待按键)。

回答by mf_

#include <unistd.h>
#include <termios.h>

char getch(void)
{
    char buf = 0;
    struct termios old = {0};
    fflush(stdout);
    if(tcgetattr(0, &old) < 0)
        perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if(tcsetattr(0, TCSANOW, &old) < 0)
        perror("tcsetattr ICANON");
    if(read(0, &buf, 1) < 0)
        perror("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if(tcsetattr(0, TCSADRAIN, &old) < 0)
        perror("tcsetattr ~ICANON");
    printf("%c\n", buf);
    return buf;
 }

Remove the last printfif you don't want the character to be displayed.

printf如果您不想显示字符,请删除最后一个。

回答by Ashish Ahuja

You can use the curses.hlibrary in linux as mentioned in the other answer.

您可以curses.h在其他答案中提到的 linux 中使用该库。

You can install it in Ubuntu by:

您可以通过以下方式在 Ubuntu 中安装它:

sudo apt-get update

sudo apt-get install ncurses-dev

sudo apt-get 更新

sudo apt-get install ncurses-dev

I took the installation part from here.

我从这里拿走了安装部分。

回答by ralf htp

As said above getch()is in the ncurseslibrary. ncurses has to be initialized, see i.e. getchar() returns the same value (27) for up and down arrow keysfor this

如上所述getch()是在ncurses图书馆。ncurses的必须被初始化,即看到的getchar()最多返回相同的值(27)和向下箭头键

回答by Ajwad Syed

I got stuck in Mac, for use of getch(), as I wanted to stop screen on the certain condition so here how I figured it:

为了使用 getch(),我被困在 Mac 中,因为我想在特定条件下停止屏幕,所以在这里我是怎么想的:

#include <stdlib.h>



int main()

{

 system( "read -n 1 -s -p \"Press any key to continue...\"" );

 return 0;

}