0
我正在做一些更大的程序的測試,這裏即時試圖移動一個空格內充滿'*'的字符數組,它工作正常,但發送了許多鍵後,左右鍵值的變化,我不知道爲什麼閱讀輸入鍵盤
這裏是我的代碼:
include <curses.h>
//#include <term.h>
#include <termios.h>
#include <stdlib.h>
#include "includes/my_list.h"
void init_term()
{
struct termios term;
if (tcgetattr(0, &term) != 0)
{
printf("Fail to get input termios\n");
// return(0);
}
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &term) != 0)
{
printf("Fail to set input termios\n");
// return(0);
}
}
t_info change_dir(t_info info, int flag)
{
if(flag == 0)
info.pos++;
else if (flag == 1)
info.pos--;
info.tab[info.pos] = ' ';
return(info);
}
int main()
{
int c;
int i; int pos;
t_info info;
// tgetent(NULL, "xterm");
info.tab = malloc(sizeof(*info.tab) * 10);
i = 0;
info.pos = 0;
init_term();
while (read(0, &c, sizeof(int)) != 0)
{
printf("c = %d\n", c);
// sleep(1);
while(i < 9)
{
info.tab[i] = '*';
i++;
}
i = 0;
if(c == 4479771)
{
printf("left ok1\n");
info = change_dir(info, 1);
}
else if (c == 4414235)
info = change_dir(info, 0);
printf("%s\n", info.tab);
}
}