-1

Continuous input code with arrow keys. Why is output repeatedly indented?

I'm writing on C using the lncurses library. I need to get continuous input with the arrow keys but my output is all weird and intended. I tried swapping \n with \r, but then it doesn't output anything at all even tho its registering key presses.

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>

#include <pthread.h>

void *input(void *arg)
{
printf("Thread running\r\n");
int ch = 0;

while(1)
{
    ch = getch();
    switch(ch)
    {
        case KEY_UP : 
            printf("up\n");
            break;
        case KEY_DOWN :
            printf("down\r");
            break;
        case KEY_LEFT :
            printf("left\r");
            break;
        case KEY_RIGHT:
            printf("right\r");
            break;
    }

}
return NULL;
}


void initcurses(); 
int main(int argc, char *argv[])
{
    //Initialise ncurses library functions
    initcurses();

    pthread_t t_input;
    pthread_create(&t_input, NULL, input, NULL);
    pthread_join(t_input, NULL);
}

void initcurses()
{
    //Initialise library
    initscr();
    //Enable control characters
    cbreak();
    //Disable getch echoing
    noecho();
    //Flush terminal buffer
    intrflush(stdscr, TRUE);
    //Enable arrow keys
    keypad(stdscr, TRUE);
}

I expect to see which key is pressed on a new line every time. Instead they are indented.

The code should be enough to reproduce the result. Compile with cc -pthread -o file file.c -lncurses

Also some notes: KEY_UP is the only thing that will have any output due to the \n character? Any other keys will be printed after UP has been pressed after them.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Nick
  • 54
  • 6
  • 3
    `"\r"` versus `"\n"`? – Some programmer dude Apr 08 '19 at 13:38
  • what do you mean @Someprogrammerdude – Nick Apr 08 '19 at 13:39
  • 1
    Seems to be working as expected, `"up\n"` will create a new line without carriage return, `"down\r"` will do a carriage return in the same line. – vgru Apr 08 '19 at 13:39
  • @Groo so you are not getting the tabulations in the console ? – Nick Apr 08 '19 at 13:40
  • @Nik: I haven't run your program, I am saying that the output looks as I would expect from your code. – vgru Apr 08 '19 at 13:41
  • 1
    @Groo ugh it seems to have been an easy solution. When you said it does what it's supposed to I tried doing \n\r and now it goes to the new line and properly starts from the beginning. – Nick Apr 08 '19 at 13:42
  • Possible duplicate of [ncurses: strange line formatting](https://stackoverflow.com/questions/44750687/ncurses-strange-line-formatting) – Thomas Dickey Apr 09 '19 at 09:16

1 Answers1

0

As @Groo pointed out, the program was doing what I told it to do.

Using \n made a new line exactly after the output so it needed a \r carriage return to properly start it from the beginning.

Swapping \n or \r to \n\r has the desired effect.

Nick
  • 54
  • 6