Is there a way to get text as soon as possible without waiting for a newline?

前端 未结 4 1190
孤独总比滥情好
孤独总比滥情好 2021-01-26 04:04

I\'m using C. I wrote a very simpe program which prints back the input, using getchar() and putchar() or printf(). Is there any way to make it so as soon as the user types one k

4条回答
  •  轮回少年
    2021-01-26 04:35

    On Linux you can take over the terminal:

    #include 
    #include 
    #include 
    
    system("stty raw"); /* raw output to terminal, direct feedback */
    system("clear"); /* clear screen */
    
    printf("Press a key");
    answer = getchar();
    
    system("stty cooked"); /* revert back*/
    

提交回复
热议问题