当用户按下ESC时终止用户输入(例如,fgets())

时间:2015-04-16 07:04:12

标签: c windows user-input keyboard-events

我正在Windows中编写一个简单的C控制台应用程序,它在无限循环中获取用户输入。

我希望应用程序处于“阅读模式”并继续阅读,直到用户按下退出键(例如,转义)。

如果按下特定的特殊键,我如何接收用户输入并终止它?

示例:

while (1)
{
    // How do I stop loop and input with a specific key hit, e.g., ESC?
    fgets(buff, 255, stdin);
    printf("%s\n", buff);
}

1 个答案:

答案 0 :(得分:3)

你可能正在寻找

while(!kbhit())

或者您可以使用getch()中包含的#include <conio.h>getchar()中包含的#include <stdio.h>通常用于 Enter 键。

相关问题