Thread: while getch question

  1. #16
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    Thanks anduril, that example certainly works, though curses seems to make the rest of my program behave quite strangely. As I mentioned this is a modification to a large, exisiting bit of code.

    I have put your curses example at the begining of my main loop (after removing the 'return 0', of course). I was hoping that afer exiting the while loop and calling endwin(), the rest of my code could run as before, using 'printf' to display strings to the user. However, it seems that I can only do this now by calling refresh, and even then this doesn't always work if a time delay is involved in generating the string (when polling a sensor for example). Worst of all is that my program now hangs occasionally and doesn't respond to Ctrl+C.

    So now I have two questions,

    1. Is it possible to use curses.h and getch for a while loop, then 'switch it off', so that I can go back to using printf? endwin() doesn't seem to do this...

    2. Is there another way to achieve the goal I want (of entering keystrokes while running a while loop) that doesn't mean changing the way I display data on the screen (refreshing doesn't seem to work with time delays). Perhaps something other than getch and curses.h.

    Many Thanks,

    Ad

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Basic unbuffered input:
    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <sys/select.h>
    
    int my_getch()
    {
        termios oldt;
        tcgetattr(STDIN_FILENO, &oldt);
        
        termios newt = oldt;
        newt.c_lflag &= ~(ICANON | ECHO);
        newt.c_cc[VMIN] = 1;
        newt.c_cc[VTIME] = 0;
    
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    
        unsigned char c;
        ssize_t nread = read(STDIN_FILENO, &c, 1);
        
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    
        return c;
    }
    gg

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Ad_robot View Post
    Thanks anduril, that example certainly works, though curses seems to make the rest of my program behave quite strangely. As I mentioned this is a modification to a large, exisiting bit of code.

    I have put your curses example at the begining of my main loop (after removing the 'return 0', of course). I was hoping that afer exiting the while loop and calling endwin(), the rest of my code could run as before, using 'printf' to display strings to the user. However, it seems that I can only do this now by calling refresh, and even then this doesn't always work if a time delay is involved in generating the string (when polling a sensor for example). Worst of all is that my program now hangs occasionally and doesn't respond to Ctrl+C.
    Oh, the joys of raw, unbuffered I/O! One of the things that goes along with unbuffered input is that you often get to handle control characters, backspacing, etc yourself.

    1. Is it possible to use curses.h and getch for a while loop, then 'switch it off', so that I can go back to using printf? endwin() doesn't seem to do this...
    You can use printf in curses. I'm not sure, but I think it's less desirable because it doesn't behave well in regards to curses screens, windows, panes, etc. Also, I'm not sure you can just "turn curses on then back off" for this, because I think curses uses the alternate screen, so it does funky stuff with the display when it goes back to the primary screen.

    2. Is there another way to achieve the goal I want (of entering keystrokes while running a while loop) that doesn't mean changing the way I display data on the screen (refreshing doesn't seem to work with time delays). Perhaps something other than getch and curses.h.
    If you just wan't unbuffered input (don't wait for a newline to process input), look into the setvbuf function. I've never used it before, so I'm not sure how successful you'll be, but it may do what you need.

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> If you just wan't unbuffered input (don't wait for a newline to process input), look into the setvbuf function.
    setvbuf() won't help. Data from the OS to the CRT will still only be available once an entire line is available. setvbuf() only controls buffering at the CRT level.
    http://pubs.opengroup.org/onlinepubs...l#tag_11_01_06

    gg

  5. #20
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    Thanks for your help guys. Unfortunately I couldn't get my_getch() working today (some issues with termios.h on the remote platform) which was a shame as it is a nice solution. I'm not back at work until the new year so am not going to find the problem until then. When I start writing my own code for this thing I'll probably end up using curses.h but for now my_getch is a cool solution.

    One thing is for sure, this isn't as simple as I expected it to be!

    Thanks again,

    Ad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. a question about putch and getch
    By jackhasf in forum C Programming
    Replies: 3
    Last Post: 10-31-2009, 03:07 PM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  4. simple question -getch \n and \r
    By GanglyLamb in forum C Programming
    Replies: 1
    Last Post: 06-10-2003, 08:12 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

Tags for this Thread