i have an interesting problem here. I want the console to be able to pause and then press any key to continue, but if you hold the key down, i don't want it to read that as the key to be continued until you press it, or another, key.
ideally this would be if you hold the key forever it won't matter. But if it has to be limited to time that would be fine.
I've been working with just limiting it to time, but the main problem for me is that getch() doesn't return '\n' for the enter press. I'm using the conio.h header for getch(). What does it return?
void pause()
{
//requires <iostream> <time.h> <conio.h>
clock_t pause;//pause is equal to 1 second after pause() was called
pause = clock() + 1 * CLOCKS_PER_SEC;
std::cout << "\nPress any key to continue...";//default text
char c = getch();//grab key press
//checker
if(c == '\n'){std::cout << "true";}
else(std::cout << "false"); //always returns false
//what i want it to do is
do
{
//check if you pressed enter, (usually automatically due to cin requiring enter)
if(c == '\n')
{
//if you did, and it's still less than 1 second since pause() was called
//^ this is checked by the while statement ^
//loop to this statement again
//bonus: check if you hit enter twice, the second breaking the while statement
}
}
while(clock() < pause);
std::cout << "\n\n";
}
The enter key is a '\r'. Windows systems translate that to "\r\n". Unix systems translate that to '\n'.
BTW, what you want to do is some special keyboard handling, and cannot be done without using system-specific stuff. Since you are on Windows, use the Windows Console Functions.