如何修复不需要的curses输出缩进

问题描述 投票:-1回答:1

带箭头键的连续输入代码。为什么输出重复缩进?

我正在使用lncurses库在C上编写。我需要使用箭头键进行连续输入,但我的输出很奇怪且有意。我尝试用\n交换\r,但是当它注册按键时它根本不输出任何东西。

#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);
}

我希望每次都看到哪一个键被按下了新的一行。相反,它们是缩进的。

代码应该足以重现结果。用cc -pthread -o file file.c -lncurses编译

还有一些注意事项:由于KEY_UP字符,\n是唯一具有任何输出的东西?在按下UP之后,将打印任何其他键。

c linux curses
1个回答
0
投票

正如@Groo指出的那样,该程序正在按照我的要求去做。

使用\ n在输出之后完成一个新行,因此它需要一个\ r \ n回车来从头开始正确启动它。

将\ n或\ r \ n转换为\ n \ r \ n具有所需的效果。

© www.soinside.com 2019 - 2024. All rights reserved.