在linux中如何用C语言实现读取“上下左右、ESC”键

s废话不多说,直接上代码!

#include 
#include 
#include 

//分配内存的大小
#define     SIZE    10
//定义按键们的宏
#define     ESC     "\033"
#define     UP      "\033[A"
#define     DOWN    "\033[B"
#define     LEFT    "\033[D"
#define     RIGHT   "\033[C"

int main()
{
    char *get = (char*)malloc(SIZE);

    for ( ; ; ) 
    {   
        fgets(get, SIZE, stdin);
        /*    用fgets()函数从stdin中读取字符串时,会自动在字符串末尾追加"\n",这里将末尾字符改为"\0"    */
        get[strlen(get) - 1] = '\0';

        if (!strcmp(get, ESC))
            printf("This is \"ESC\" button!\n");
        if (!strcmp(get, UP))
            printf("This is \"UP\" button!\n");
        if (!strcmp(get, DOWN))
            printf("This is \"DOWN\" button!\n");
        if (!strcmp(get, LEFT))
            printf("This is \"LEFT\" button!\n");
        if (!strcmp(get, RIGHT))
            printf("This is \"RIGHT\" button!\n");
    }   

    return 0;
}

运行效果如图:
在linux中如何用C语言实现读取“上下左右、ESC”键_第1张图片

你可能感兴趣的:(在linux中如何用C语言实现读取“上下左右、ESC”键)