Non-Blocking i/o in c? (windows)

牧云@^-^@ 提交于 2019-12-06 11:34:18

From the documentation for _kbhit():

The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer. The program can then call _getch or _getche to get the keystroke.

So, in your loop:

while (true) {
    // ...
    if (_kbhit()) {
        char c = _getch();
        // act on character c in whatever way you want
    }
}

So, you can still use _getch(), but limit its use to only after _kbhit() says there is something waiting. That way it won't block.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!