[指南]liunxc键盘操纵 完成字符串移动
通过信号控制屏幕字符串移动。如下图:
a) 输入a,d,w,s
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
示左,右,上,下。q退出。
b) 要求字符串移动不能出框。
#include
#include
#include
#include #include
#include
#include
/*
清屏:
*/
void clear_screen()
{
system("echo -ne \"\033[2J\""); }
/*
光标定位:
y:行坐标,x:列坐标 */
int G_y=2,G_x=2;
int setyx(int y,int x)
{
struct termios tm, tm_old;
int fd = STDOUT_FILENO;
char c[20];
if(tcgetattr(fd, &tm) < 0)
return -1;
tm_old = tm;
cfmakeraw(&tm);
if(tcsetattr(fd, TCSANOW, &tm) < 0)
return -1;
fflush(stdout);
sprintf(c,"echo -ne \"\\033[%d;%dH\"",y,x);
system(c);
fflush(stdout);
if(tcsetattr(fd, TCSANOW, &tm_old) < 0)
return -1;
return 0;
}
//linux中getch的实现 int getch(void)
{
struct termios tm, tm_old;
int fd = STDIN_FILENO, c;
if(tcgetattr(fd, &tm) < 0)
return -1;
tm_old = tm;
cfmakeraw(&tm);
if(tcsetattr(fd, TCSANOW, &tm) < 0)
return -1;
c = fgetc(stdin);
if(tcsetattr(fd, TCSANOW, &tm_old) < 0)
return -1;
return c;
}
void show()
{
printf("********************\n");
int i=0;
int j=0;
for(j=0;j<10;j++)
{
for(i=0;i<20;i++)
{
if(i==0)
printf("*");
else if(i==19)
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("********************\n"); }
int main()
{
setyx(0,0);
show();
setyx(G_y,G_x);
printf("abc\n");
int input;
input = getch();
while(input!=113)
{
clear_screen();
switch(input)
{
case 97:G_x--;break;
case 115:G_y++;break;
case 100:G_x++;break;
case 119:G_y--;break;
default:break;
}
setyx(0,0);
show();
if(((G_x>=2)&&(G_x<=17))&&((G_y>=2)&&(G_y<=11)))
{
setyx(G_y,G_x);
printf("abc\n");
}
else
{
switch(input)
{
case 97:G_x++;break;
case 115:G_y--;break;
case 100:G_x--;break;
case 119:G_y++;break;
default:break;
}
setyx(G_y,G_x);
printf("abc\n");
}
input = getch();
}
return 0;