感谢您抽出宝贵时间阅读此问题!
我有一个正在使用Dev-C ++作为IDE和Windows作为操作系统的项目。由于项目由于大小限制和当前知识而受到复杂性的限制,因此我宁愿避免使用GetAsyncKeyState
和线程,除非可以使用相当简单的方法(少于10行)。
我的程序当前使用Text
函数通过字符串显示文本,并将其输出到控制台窗口。我想实现一个功能,该功能允许用户按下一个键,并允许打印文本,而无需等待该功能读取整个字符串。
“”欢迎使用...“ [用户在逐字符缓慢显示文本时按下空格键]
“欢迎使用我的程序!这是我开发的!” [显示整个字符串]
void Text(string input)
{
int x = 0;
for(int i = 0; i <= 3; i++) //decides when to stop running based on number of strings
{
while (input[x] != '\0')
{
if(input[x] == '.' || input[x] == '!')
{
cout << input[x];
Sleep(375);
cout << " ";
x++;
}
else if(input[x] == '*')
{
Sleep(375);
cout << endl;
x++;
}
else if(input[x] == '~')
{
Sleep(2000);
system("CLS");
x++;
}
else
{
cout << input[x];
Sleep(75);
x++;
}
}
}
}
如果需要任何其他信息,请在下面留下评论!再次感谢您的宝贵时间。
不,您不能以便携式的方式。原因是通常的控制台上的标准I / O等待一行完成,因此您不能在键入单个字符后立即捕获它们。
不过,您可以使用非标准功能来实现!参见Capture characters from standard input without waiting for enter to be pressed