C 如何修复不需要的诅咒输出缩进
带箭头键的连续输入代码。为什么输出会重复缩进 我正在用lncurses库写C。我需要用箭头键获得连续的输入,但我的输出很奇怪,而且是有意的。我尝试将C 如何修复不需要的诅咒输出缩进,c,linux,curses,C,Linux,Curses,带箭头键的连续输入代码。为什么输出会重复缩进 我正在用lncurses库写C。我需要用箭头键获得连续的输入,但我的输出很奇怪,而且是有意的。我尝试将\n与\r交换,但即使按下注册键,它也不会输出任何内容 #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #in
\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.c-lncurses编译
还有一些注意事项:KEY\u UP
是由于\n
字符而唯一有任何输出的东西吗?在按下UP
键后,将打印任何其他键。正如@Groo指出的,程序正在执行我告诉它的操作
使用\n在输出之后创建了一个新行,因此需要一个回车符\r才能正确地从头开始
交换\n或\r到\n\r具有所需的效果。“\r”
与“\n”
?什么意思@someProgrammerdude似乎按预期工作,“up\n”
将创建一个没有回车的新行,“down\r”
将在同一行中执行回车。@Groo所以您没有在控制台中获取表格?@Groo这似乎是一个简单的解决方案。当你说它做了它应该做的事情时,我试着做了\n\r,现在它转到新的行,正确地从一开始就开始了。不仅仅是Groo,一些程序员以前说过;-)我想是吧?直到格罗提起这件事,我才完全明白他的意思。但你是对的