#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    46
    Rep Power
    7

    Question How to use getch in Linux?


    getch is in the conio library i know, however in linux it doesnt work as far as iv tried. My question is is there a function i could write to use it in linux, or how can i use it in linux in better words. Iv seen a couple of guys in advanced classes done it but i got no idea
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Posts
    1,014
    Rep Power
    788
    Why not use getchar()? "Get a byte from the stdin stream." getch is not a standardized function.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    46
    Rep Power
    7
    Originally Posted by Thr3ddy
    Why not use getchar()? "Get a byte from the stdin stream." getch is not a standardized function.
    I know bout getchar, the thing is i still need to press enter. And i want something that as soon as they press anything to continue, for instance a calculator program. press any key to continue
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I hope that this will point you in the right direction: http://members.aol.com/dsc30574/sock...king.html#KYBD
    (rumor has it that all member sites hosted by AOL will go away after the end of this month, so if this page contains useful information you might want to save it locally)

    To my knowledge, basically for UNIX/Linux there are two basic options:
    1. Low-level terminal commands (stty)
    2. curses and ncurses, which are wrappers for stty commands.

    IOW, you need to change the properties of the tty device. curses/ncurses will do this for you and manage everything -- there's also ANSI escape-sequence commands for manipulating screen display that curses/ncurses handles for you, including accounting for some non-standard behavior in some platforms.

    In the curses/ncurses section, I post some code that I researched from somewhere else. Also, further down in a kbhit section, I point you to Pete Wilson's site where he's written ports for some conio functions and I post a listing of his kbhit.c code in order to illustrate the stty functions. I haven't gone through it in detail (my Linux box at work died over a year ago and I've not set up another yet), but it appears that you basically want to set the terminal to operate in raw mode.

    With those keywords in hand, you should be able to Google for the rest of the information you need.
    Last edited by dwise1_aol; October 21st, 2008 at 01:46 PM.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    Here's something I just banged out real quick:
    Code:
    #include <curses.h>
    #include <stdio.h>
    
    int main(void)
    {
        int ch = 0;
    
        /* Basic initialization of curses lib */
        initscr();
        cbreak();
        noecho(); /* Set this for interactive programs. */
        nonl();
        intrflush(stdscr, FALSE);
        keypad(stdscr, TRUE);
        
        /* Ready to rumble! */
        printw("Type a key. Use 'q' to quit\n");
        while (ch != 'q') {
            ch = getch();
            printw("You typed: '%c' which is ASCII %d\n", (char) ch, ch);
        }
    
        /* Cleanup time */
        endwin(); 
        return 0;
    }
    Compile with:
    gcc -o yourprogram yourprogram.c -lcurses

    In the above code, getch() will wait for a keypress and return as soon as you press a key. If you need to emulate kbhit() behavior (i.e.) check if any key has been pressed and do something else if it hasn't, simply set nodelay() during initialization and getch() will return ERR if there is no key pressed when it runs.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    46
    Rep Power
    7
    Originally Posted by Scorpions4ever
    Here's something I just banged out real quick:
    Code:
    #include <curses.h>
    #include <stdio.h>
    
    int main(void)
    {
        int ch = 0;
    
        /* Basic initialization of curses lib */
        initscr();
        cbreak();
        noecho(); /* Set this for interactive programs. */
        nonl();
        intrflush(stdscr, FALSE);
        keypad(stdscr, TRUE);
        
        /* Ready to rumble! */
        printw("Type a key. Use 'q' to quit\n");
        while (ch != 'q') {
            ch = getch();
            printw("You typed: '%c' which is ASCII %d\n", (char) ch, ch);
        }
    
        /* Cleanup time */
        endwin(); 
        return 0;
    }
    Compile with:
    gcc -o yourprogram yourprogram.c -lcurses
    I tried running that the way said etc, and it simply doesnt work
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    Define "doesn't work". Also qualify your compiler and environment. I ran this on Ubuntu Hardy Heron using gcc 4.2.3 and it works fine.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0
    The solution to this problem can be found here

    www(dot)smashedstack.cu.cc/unbuffered-input-terminal/
    Last edited by Fa773N M0nK; November 3rd, 2012 at 08:58 AM. Reason: typos

IMN logo majestic logo threadwatch logo seochat tools logo