用信號實現鍵盤的控制

#include
#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章