synchronous keystroke reading from console application

前端 未结 3 1926
天命终不由人
天命终不由人 2021-01-29 03:01

I want to read every keystroke from a console application written in c under windows and linux immediately. Unfortunately the function gets(line) does only return a value, when

3条回答
  •  不思量自难忘°
    2021-01-29 03:43

    The following code worked for me. Thank you for pointing me into to right direction. http://bytes.com/topic/c/answers/503640-getch-linux

    #include 
    #include 
    
    int mygetch(void)
    {
    struct termios oldt,
    newt;
    int ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
    
    }
    

提交回复
热议问题