Home > database >  Strange Cursor Movements in NCurses (C)
Strange Cursor Movements in NCurses (C)

Time:10-22

I'm having issues with NCurses moving the curser to the wrong place. I have no idea why its happening. Every time I enter a key, the cursor moves 1 place further to the left until it gets to the left and just stays there. In theory, the cursor should be set to y=1, x=3 every time the loop loops.

Something of note is that when I add move(0, 0) to the end of the loop it seems to fix it but I'm just a bit confused as to why it's doing it in the first place.

#include <ncurses.h>
#include <stdlib.h>


int main(){

    initscr();
    clear();
    noecho();
    cbreak();
    nodelay(stdscr, 1);

    bool running = 1;
    int chr;
    while(running && (chr = getch()) != '\n'){
        mvaddch(1, 3, chr);
    }
    curs_set(2);

    getch();
    clrtoeol();
    refresh();
    endwin();

    return 0;
}

Edit

I should probably say what I want it to do as it seems like this is an expected result. I want to print the char at the point y=1, x=3 every time. for whatever reason, the x value changes every time the character being added changes.

Final Edit

changeing mvaddch(1, 3, chr); to if(chr != ERR) memory[2] = chr; fixed the issue.

CodePudding user response:

For whatever reason, you choose to set the nodelay option:

nodelay

  • The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.

So getch does not wait for a character to be pressed, and thus normally returns ERR. The expectation is that you never provide ERR or EOF as an argument to mvaddch; if you do that, all bets are off. On my system, it just continually overwrites with a grey box.

Try removing the call to nodelay.

  • Related