通过按键打破循环

发布于 2024-11-28 15:34:26 字数 423 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-12-05 15:34:26

您只需调用 MessageBox 即可。

You can just call MessageBox.

云胡 2024-12-05 15:34:26

很难从您的问题中看出,但听起来您正在尝试在循环的每次迭代后提示用户是否希望继续。我假设无限 for 循环位于主处理循环的末尾。

您不需要无限循环:_getch() 无论如何都会停止执行,直到按下某个键。事实上,您将条件语句包装在 for 循环中的事实是 break 的行为不像您想要的那样 - 您是 跳出无限循环,但不是主处理循环。

示例:

while(1)
{
   // Do some processing
   for (;;)
   {
      char temp;
      temp = _getch();

      if (temp == 'q')
         break;   // This will break out of the for and continue the while
   }
}

vs.

while(1)
{
   // Do some processing
   if ('q' == _getch())
      break;             // This will break out of the while
}

您还会注意到,由于 _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 a for loop is the reason that break isn't behaving like you want -- you're breaking out of the infinite loop, but not your main processing loop.

Example:

while(1)
{
   // Do some processing
   for (;;)
   {
      char temp;
      temp = _getch();

      if (temp == 'q')
         break;   // This will break out of the for and continue the while
   }
}

vs.

while(1)
{
   // Do some processing
   if ('q' == _getch())
      break;             // This will break out of the while
}

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).

诠释孤独 2024-12-05 15:34:26

您必须创建一个消息循环并等待 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.

过气美图社 2024-12-05 15:34:26

您提到这是一个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文