Thread: Using ncurses with 1 output window updating and 1 input from keyboard?

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    15

    Using ncurses with 1 output window updating and 1 input from keyboard?

    Hi.. I'm trying to write a C function that will have 2 windows, 1 the user can type whatever they want and the other window is updating data in realtime from a counter, a file, a db, or anything else... my problem is after I create my two windows and put my wscanw() in my loop with the refresh for the output window it stops refreshing the output window when it waits for input in the input window, how can I have it do both at the same time? thanks for any help.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to read input in a non-blocking manner.
    Here's one possibility.
    Enter 0 to quit.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ncurses.h>
    
    int main() {
        char buf[100] = {0}, *s = buf;
        int ch, cnt = 0, n = 1;
        WINDOW *w;
    
        if ((w = initscr()) == NULL) {
            fprintf(stderr, "Error: initscr()\n");
            exit(EXIT_FAILURE);
        }
        keypad(stdscr, TRUE);
        noecho();
        cbreak();      // disable line-buffering
        timeout(100);  // wait 100 milliseconds for input
    
        while (n != 0) {
            erase();
            mvprintw(0, 0, "cnt: %d", cnt++);
            mvprintw(1, 0, "buf: %s", buf);
            mvprintw(2, 0, "int: %d", n);
            refresh();
            
            // getch (with cbreak and timeout as above)
            // waits 100ms and returns ERR if it doesn't read anything.
            if ((ch = getch()) != ERR) {
                if (ch == '\n') {
                    *s = 0;
                    sscanf(buf, "%d", &n);
                    s = buf;
                    *s = 0;
                }
                else if (ch == KEY_BACKSPACE) {
                    if (s > buf)
                        *--s = 0;
                }
                else if (s - buf < (long)sizeof buf - 1) {
                    *s++ = ch;
                    *s = 0;
                }
            }
        }
    
        delwin(w);
        endwin();
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    15
    thank you that's exactly what I was looking for!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2012, 06:51 PM
  2. How to clear ncurses window from (n, n) to (n, n)?
    By Ravi Raj in forum C Programming
    Replies: 5
    Last Post: 05-22-2012, 11:35 PM
  3. ncurses window problem
    By fronty in forum Linux Programming
    Replies: 3
    Last Post: 09-22-2007, 09:33 AM
  4. Ncurses and WINDOW scrolling
    By birkoss in forum Linux Programming
    Replies: 0
    Last Post: 05-17-2002, 11:47 AM
  5. Keyboard input without screen output?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2001, 02:57 AM

Tags for this Thread