pausing with getch()

closed account (4Gb4jE8b)
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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";
}
closed account (4Gb4jE8b)
sadman bump
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.

http://www.google.com/search?btnI=1&q=msdn+ReadConsoleInput
You can check whether or not the key is delivering on a key press or key release, and whether or not you are getting an autorepeat press...
closed account (4Gb4jE8b)
Thanks Duoas, that helped.

Edit: you said I can find whether it's a key press or key release, but how would i do that? I'm not seeing it in the documentation.
Last edited on
Topic archived. No new replies allowed.