您的位置:首页 > 其它

用信号实现键盘的控制

2012-08-24 16:04 204 查看
#include <stdio.h>

#include <termios.h>

#include <unistd.h>

#include <signal.h>

#include "shapehead.h"

void timer(int signo)

{

printf("time : 1s\n");

alarm(1);

}

int main()

{ srand(time(NULL));

nextindex=randomshape();

drawframe();

//printnextshape();

start();

printf("\033[0m");

struct termios termios_p[1];

struct termios termios_backup[1];

tcgetattr(0, termios_p);

tcgetattr(0, termios_backup);

termios_p->c_lflag = termios_p->c_lflag & ~(ICANON | ECHO);

termios_p->c_cc[VMIN] = 1;

termios_p->c_cc[VTIME] = 0;

tcsetattr(0, TCSANOW, termios_p);

signal(SIGALRM, timer);

alarm(1);

while(1)

{

fflush(NULL);

int c = getchar();

if (c == 'q' || c == 'Q') return(0);

else if(c == 'w' || c == 'W') printf("UP\n");

else if(c == 's' || c == 'S') printf("DOWN\n");

else if(c == 'a' || c == 'A') printf("LEFT\n");

else if(c == 'd' || c == 'D') printf("RIGHT\n");

if(c == 0x5b)

{

c = getchar();

switch(c)

{

case 0x41: printf("up\n"); break;

case 0x42: printf("down\n"); break;

case 0x43: printf("right\n"); break;

case 0x44: moveleft(); break;

}

}

}

tesetattr(0, TCSANOW, termios_backup);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: