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)?
CodePudding user response:
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