【问题标题】:How to implement getch() function of C in Linux?如何在 Linux 中实现 C 的 getch() 函数?
【发布时间】:2010-07-18 17:40:17
【问题描述】:

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

【问题讨论】:

标签: c gcc getch conio


【解决方案1】:

试试这个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 标志吗?它的作用是什么?
【解决方案2】:

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

【讨论】:

  • 回显到屏幕并不是getch()getchar() 之间的唯一区别。 getch() 在从缓冲区读取之前不会等待回车。例如。要使用getchar() 输入“a”,您必须输入a[ENTER]。使用getch(),您只需要输入'a'。
【解决方案3】:

【讨论】:

  • 同意,否决票是不合理的——curses 实际上提供了一个getch() 函数。
  • 也许人们认为这是矫枉过正
  • @Isaac 如果您正在使用conio.h 的一部分,那么您可能正在使用其他部分。如果你正在移植一个大量使用conio.h 的应用程序,那么curses 可能是最好的选择。我认为downvote更像是只看链接的答案。
  • 我对此投了反对票,因为它只是一个链接的答案,而且,以目前的形式,它可能应该是评论而不是答案。虽然这实际上可能是一个相关链接,但如果您实际上将一段文本直接添加到您的帖子中,我会赞成,关于 curses 库以及为什么您认为它与解决 OPs 问题相关。
【解决方案4】:

@987654321@ 似乎包含在curses library 中。

【讨论】:

    【解决方案5】:

    根据这些解决方案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);
    }
    

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

    【讨论】:

      【解决方案6】:

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

      __extern int caca_conio_getch (void)
      

      【讨论】:

        【解决方案7】:

        如果由于任何原因你不能使用诅咒,试试这个:

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

        【讨论】:

          【解决方案8】:

          在 Unix 中,getch() 是 ncurses 库的一部分。但是我为 this question 写了一个解决方法,它可以让你使用类似 getch 的功能,而无需其他的诅咒包袱。

          【讨论】:

            【解决方案9】:

            conio.h 只在 Dos 中,

            对于 linux,使用

            sudo apt-get install libncurses-dev
            

            & 然后

            -lncurses
            

            // 在 IDE 中,你必须链接它: 例如:代码块,设置 -> 编译器 -> 链接器设置,并添加 'ncurses'

            【讨论】:

              【解决方案10】:

              getch()libcurses 中。 curses 的使用有点复杂,因为它与底层终端有很深的链接并且必须被初始化。带有 libcurses 初始化的 curses getch() 的工作示例位于 getchar() returns the same value (27) for up and down arrow keys

              【讨论】:

                【解决方案11】:

                你也可以像这样在linux中使用系统命令来控制终端

                char getch()    {
                
                        char c;
                
                        system("stty raw -echo");
                
                        c = getchar();
                
                        system("stty -raw echo");
                
                        return c;
                
                }
                

                此功能不需要用户按回车键,并从用户那里获取输入而不回显它需要您将 stdlib.h 库添加到您的代码中

                注意:此功能仅适用于基于 UNIX 的操作系统

                我们将不胜感激任何改进或指出代码中的任何问题

                问候

                【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-04-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-06-01
                相关资源
                最近更新 更多