Trying to read keyboard input without blocking (Windows, C++)

前端 未结 2 1526
南旧
南旧 2020-12-21 03:03

I\'m trying to write a Windows console application (in C++ compiled using g++) that will execute a series of instructions in a loop until finished OR until ctrl-z (or some o

2条回答
  •  庸人自扰
    2020-12-21 03:46

    If G++ supports conio.h then you could do something like this:

    #include 
    #include 
    
    void main()
    {
        for (;;)
        {
            if (kbhit())
            {
                char c = getch();
                if (c == 0) {
                    c = getch(); // get extended code
                } else {
                    if (c == 'a') // handle normal codes
                        break;
                }
            }
        }
    }
    

    This link may explain things a little more for you.

提交回复
热议问题