while statements and getch()

closed account (4Gb4jE8b)
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
30
31
void pause()
{
	//requires <iostream> <time.h> <conio.h>
	clock_t pause;//pause is equal to 1 second after pause() was called
	pause = clock() + 0.5 * CLOCKS_PER_SEC;
	std::cout << "\nPress any key to continue...";//default text
	char c = getch();//grab any key press
	
	//what i want it to do is 
	do
	{
//check if you pressed enter and held it for that little too long (usually due to cin requiring enter)
		if(c == '\r')
		{
			//if you did, and it's still less than 1 second since pause() was called
			//^ this is checked by the while statement ^
			char a;
			do
			{
				if(clock()>pause){break;}
				a = getch();
//check if you hit enter (presumably) twice, the second breaking the while statement
				break;
			}
			while(clock()<pause);
		}
		break;//if you didn't press enter, just break it
	}
	while(clock() < pause);
	std::cout << "\n\n";
}


Sorry for the long comments, but they explain the basis of what i want to do. Here's my problem, regardless of while(clock()<pause) if i hit the innermost do statement's getch() the user has to press a key again. I would have though that the while statement would have ignored that at a point. Is there anyway i can make it ignore that after clock()>pause? Thanks for the help in advance
Let me just be sure of what you want here.

You're interested in having a way to check if a key (specifically enter) has been held down for more than a certain amount of time, right?

-Albatross
closed account (4Gb4jE8b)
In essence yes, if you used enter to go forward after "press any key" I want the program to check if it's been long enough since the pause function was called, so that it's not a matter of you holding enter for that millisecond too long and it being put into getch as well.

The problem here is the differentiation between having held the key and it being put into getch and having pressed the key and have it put into getch. if it's held, i want it to not accept the key, which i tried to do through the second getch() and am now realizing that it wouldn't actually have stopped it from being held in the first place... where as if it's pressed i want it to accept the key and move forward.

ergh... i think this is too much for a beginner like me.
are you using this to prevent people from speeding through your story sequence sort of speak. just curious.
As far as I know, conio.h varies considerably between platforms. I think you should maybe consider something else to detect your keypresses. I know the Windows API has something, and I think so does PDCurses...

-Albatross
closed account (4Gb4jE8b)
@acorn, in essence yes.

@albatross, but considering anyone i'm giving this to, i'll be giving it to in binary form, will it matter? And i'll look through the API documentation, but it's often very confusing for me.
If you'll be giving it in binary form, then I guess that you don't expect it to be usable across several platforms... I would suggest something cross-platform over a system-specific API in this case (PDCurses; if it doesn't have keypress detection which I think it does, let me know and I'll see what else I can find).

I don't know how much getch() varies between EDIT: implementations, but after you compile it, that variance won't matter, no.

Happy coding!

-Albatross
Last edited on
closed account (4Gb4jE8b)
no i'm not expecting full portability, aiming for windows xp, vista, and 7. I know it works on vista and 7. Haven't tried xp yet. I'll look into PDCurses, Thanks albatross!
Topic archived. No new replies allowed.