1. 程式人生 > >從標準輸入裡面不需要按回車得到一個輸入字元 (C程式碼)

從標準輸入裡面不需要按回車得到一個輸入字元 (C程式碼)



#include <termios.h>
#include <unistd.h>
#include <stdio.h>


int main(void)
{
char c;
    struct termios tTTYState;
 
    //get the terminal state
    tcgetattr(STDIN_FILENO, &tTTYState);
 
    //turn off canonical mode
    tTTYState.c_lflag &= ~ICANON;
    //minimum of number input read.
    tTTYState.c_cc[VMIN] = 1;   /* 有一個數據時就立刻返回 */


    //set the terminal attributes.
    tcsetattr(STDIN_FILENO, TCSANOW, &tTTYState);


while (1)
{
c = fgetc(stdin);  /* 會休眠直到有輸入 */
printf("get char : %c\n", c);

}

return 0;
}