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

前端 未结 2 1520
南旧
南旧 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:44

    Try ReadConsoleInput to avoid cooked mode, and GetNumberOfConsoleInputEvents to avoid blocking.

    0 讨论(0)
  • 2020-12-21 03:46

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

    #include <conio.h>
    #include <stdio.h>
    
    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.

    0 讨论(0)
提交回复
热议问题