Getch() in Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GLCODER
    New Member
    • Jun 2006
    • 2

    Getch() in Linux

    Does anyone know of a getch() equivalent that exists in linux?
    I have just started doing some basic programming and am currently running Knoppix on CD.
    So I'm not sure what kind of file library I have available.

    Any help would be greatly appreciated;

    GL
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What programming language are you talking about? C/C++?

    In my MSVC I can find

    getc
    getchar
    _getch

    getc and getch are standard library functions and should be found in any C/C++ implementation.

    _getch is a platform implementation function (denoted by the _ at the start of the name), a function provide by the platform in question to support the rest of the c standard library.

    The closest standard library equivilent to _getch is getchar.

    Comment

    • justaguy1010
      New Member
      • Oct 2008
      • 1

      #3
      Originally posted by Banfa
      What programming language are you talking about? C/C++?

      In my MSVC I can find

      getc
      getchar
      _getch

      getc and getch are standard library functions and should be found in any C/C++ implementation.

      _getch is a platform implementation function (denoted by the _ at the start of the name), a function provide by the platform in question to support the rest of the c standard library.

      The closest standard library equivilent to _getch is getchar.

      Put the following text in to Gedit and save as getch.h
      to use the function use mygetch()

      works like a charm
      Code:
      /
      
      #include <termios.h>
      #include <unistd.h>
      
      int mygetch(void)
      {
      struct termios oldt,
      newt;
      int ch;
      tcgetattr( STDIN_FILENO, &oldt );
      newt = oldt;
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr( STDIN_FILENO, TCSANOW, &newt );
      ch = getchar();
      tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
      return ch;
      }
      /solution provided by kermi3 from this web posting http://cboard.cprogramming.com/archi...p/t-27714.html
      Last edited by zmbd; May 17 '19, 08:56 PM.

      Comment

      • Emmett Brown
        New Member
        • May 2019
        • 1

        #4
        Originally posted by justaguy1010
        Put the following text in to Gedit and save as getch.h
        to use the function use mygetch()

        works like a charm

        /

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

        int mygetch(void)
        {
        struct termios oldt,
        newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
        }


        /solution provided by kermi3 from this web posting http://cboard.cprogramming.com/archi...p/t-27714.html


        Actually the compiler says that getchar() "is not declared in this scope".

        I found out that you have to include the <cstdio> library.

        Also, it appears this function can't read the '\r' character. (The enter key).

        Anyway, thank you so much! you helped me a lot!
        Last edited by Emmett Brown; May 17 '19, 02:02 PM.

        Comment

        Working...