通过按键打破循环
我正在编写一个 Win32 GUI 应用程序,它有一个循环,我想仅在按下按键时重新启动。主循环已经在运行,我不能简单地重新启动它,所以我必须插入一个“挂起”点,用户可以手动退出该点。下面的代码代表了我在主循环末尾放置的内容。它应该通过将程序放入无限子循环来暂停程序,只有当按下字母“q”时才能中断该子循环。
for (;;)
{
char temp;
temp = _getch();
if (temp == 'q')
{
break;
}
}
这成功地使程序挂起,但按“q”不会结束循环。我知道使用 cin.ignore() 或 cin.get() 会更好,但由于某种原因,当我将 iostream 添加到标头列表时,我收到一堆错误,所以我目前正在尝试使用 _getch( )与 conio.h 标头。
非常感谢任何帮助。
I am writing a Win32 GUI app which has a loop which I would like to restart only when a keypress is made. The main loop is already running, and I can't simply restart it, so I have to insert a 'hang' point which the user can manually break out of. The code below represents what I have put at the end of my main loop. It is supposed to pause the program by putting it into an endless sub-loop which can only be broken when the letter 'q' is pressed.
for (;;)
{
char temp;
temp = _getch();
if (temp == 'q')
{
break;
}
}
This successfully makes the program hang, but pressing 'q' does nothing to end the loop. I understand that using cin.ignore() or cin.get() would be preferable, but for some reason when I add iostream to the header list, I get a bunch of errors, so I am currently trying to do it using _getch() with the conio.h header.
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需调用
MessageBox
即可。You can just call
MessageBox
.很难从您的问题中看出,但听起来您正在尝试在循环的每次迭代后提示用户是否希望继续。我假设无限
for
循环位于主处理循环的末尾。您不需要无限循环:
_getch()
无论如何都会停止执行,直到按下某个键。事实上,您将条件语句包装在for
循环中的事实是break
的行为不像您想要的那样 - 您是跳出无限循环,但不是主处理循环。
示例:
vs.
您还会注意到,由于 _getch() 返回一个
char
,因此您可以只测试函数的返回代码(除非您需要存储输入以供以后使用)。It's hard to tell from your question, but it sounds like you're trying to prompt the user after each iteration of a loop if they wish to continue. I assume that the infinite
for
loop is at the end of your main processing loop.You don't need the infinite loop:
_getch()
will halt execution anyway until a key is pressed. In truth, the fact that you've wrapped your conditional in afor
loop is the reason thatbreak
isn't behaving like you want -- you'rebreak
ing out of the infinite loop, but not your main processing loop.Example:
vs.
You'll also notice that since _getch() returns a
char
, you can just test the return code of the function (unless you need to store the input for later use).您必须创建一个消息循环并等待 keydown 消息到达。 尝试检查此示例。
在 Windows GUI 应用程序中,没有可从控制台读取的输入。
You have to create a message loop and wait until a keydown message arrives. Try to check this example.
In a windows GUI application there is no input to read from the console.
您提到这是一个 Win32 GUI 应用程序。在进入循环时,您是否创建了任何可能具有输入焦点的窗口?如果是,您应该尝试捕获 WM_KEYDOWN/< a href="http://msdn.microsoft.com/en-us/library/ms646280%28VS.85%29.aspx" rel="nofollow">WM_KEYUP 消息。如果是控制台应用程序,请查看Console功能特别ReadConsole。
山姆
You mentioned this is a Win32 GUI application. At the time of entering the loop, have you created any Windows that might have the input focus? if yes you should try trapping WM_KEYDOWN/WM_KEYUP messages. If it is a console application, look at Console Functions specially ReadConsole.
Sam