C non-blocking keyboard input

前端 未结 10 793
说谎
说谎 2020-11-22 05:59

I\'m trying to write a program in C (on Linux) that loops until the user presses a key, but shouldn\'t require a keypress to continue each loop.

Is there a simple wa

10条回答
  •  有刺的猬
    2020-11-22 06:23

    You probably want kbhit();

    //Example will loop until a key is pressed
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        while(1)
        {
            if(kbhit())
            {
                break;
            }
        }
    }
    

    this may not work on all environments. A portable way would be to create a monitoring thread and set some flag on getch();

提交回复
热议问题