在运行时保持对用户输入的响应,以重新启动选项

发布于 2025-02-07 03:21:12 字数 766 浏览 0 评论 0原文

我已经编程了一个小型提醒应用程序,该应用程序每隔几秒钟发出哔哔声:

#include <windows.h>
#include <stdio.h>

int main()
{
    int interval;
    printf("How often shall I remind you?\n");
    printf("Every x seconds: ");
    scanf("%d", &interval);

    if (interval < 0 || interval > 300)
    {
        printf("Invalid interval. Try 0 < interval < 300.");
        return 1;
    }

    int numofbeeps = 3600/interval;
    printf("Good. I will beep every %d seconds (that makes %d times) over the next 60 minutes.\n", interval, numofbeeps);

    for(int ctr=0 ; ctr < numofbeeps ; ctr++)
    {
        Sleep(interval*1000);
        Beep( 1000, 750 ); 
    }
    return 0;
}

是否有一种方法可以连续扫描用户输入(即使在 goto - 使用来重新启动提醒循环)?

I have programmed a small reminder application which beeps every few seconds as specified by the user:

#include <windows.h>
#include <stdio.h>

int main()
{
    int interval;
    printf("How often shall I remind you?\n");
    printf("Every x seconds: ");
    scanf("%d", &interval);

    if (interval < 0 || interval > 300)
    {
        printf("Invalid interval. Try 0 < interval < 300.");
        return 1;
    }

    int numofbeeps = 3600/interval;
    printf("Good. I will beep every %d seconds (that makes %d times) over the next 60 minutes.\n", interval, numofbeeps);

    for(int ctr=0 ; ctr < numofbeeps ; ctr++)
    {
        Sleep(interval*1000);
        Beep( 1000, 750 ); 
    }
    return 0;
}

Is there a way to continuously scan for user input (even during sleep times) and, upon a certain input, take a certain action (e.g. restart the reminder loop by overwriting a variable and some goto-use)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

Spring初心 2025-02-14 03:21:12

一个过程可以在其中一个等待功能中指定控制台输入缓冲区句柄,以确定何时有未读游戏机输入。当输入缓冲区不为空时,请发出控制台输入缓冲区手柄的状态。

DWORD wait = WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), interval*1000);
if (wait == WAIT_OBJECT_0)
  // read input
else if (wait == WAIT_TIMEOUT)
  // Beep?
else
  // Handle error

A process can specify a console input buffer handle in one of the wait functions to determine when there is unread console input. When the input buffer is not empty, the state of a console input buffer handle is signaled.

DWORD wait = WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), interval*1000);
if (wait == WAIT_OBJECT_0)
  // read input
else if (wait == WAIT_TIMEOUT)
  // Beep?
else
  // Handle error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文