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

后端 未结 6 1985
南方客
南方客 2020-11-22 05:53

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

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

相关标签:
6条回答
  • 2020-11-22 06:15
    #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 printf if you don't want the character to be displayed.

    0 讨论(0)
  • 2020-11-22 06:23

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

    0 讨论(0)
  • 2020-11-22 06:26
    #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
    
    0 讨论(0)
  • 2020-11-22 06:26

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

    0 讨论(0)
  • 2020-11-22 06:30

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

    You can install it in Ubuntu by:

    sudo apt-get update

    sudo apt-get install ncurses-dev

    I took the installation part from here.

    0 讨论(0)
  • 2020-11-22 06:30

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

    0 讨论(0)
提交回复
热议问题