Stop timer when key is pressed

I'm trying to write a basic timer program for kicks. Everything works, but I can't figure out how to make the timer stop when a key is pressed. A specific key or any key will do just fine. The code so far:

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
#include "iostream"
#include "windows.h"
using namespace std;

int main()
{
    int a = 0; //seconds
    int b = 0; //minutes
    cout << "Press enter to start the timer.";
    cin.ignore();
    do
    {
        cout << b << ":";
        if (a < 10)
        {
            cout << "0";
        }
        cout << a << "\n";
        a++;
        if (a == 60)
        {
            a = 0;
            b++;
        }
        Sleep(10); //Sped up so I don't have to wait forever. Will be changed to 1000 eventually.
    }while (b < 2); //To make sure the program stops eventually
    return (0);
}


Any other advice is appreciated.
Since you're already using <windows.h>, you can use GetAsyncKeyState() for this.

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
#include <iostream>
#include <iomanip>
#include <windows.h>

int main() {

	std::cout << "Hit enter to start timer" << std::endl;
	std::cout << "Hit 'a' to stop timer" << std::endl;
	std::cin.get();

	unsigned short seconds = 0;
	unsigned short minutes = 0;

	while (!GetAsyncKeyState(0x41)) {
	//while 'a' is not being pressed
		seconds++;
		if (seconds == 60) {
			seconds = 0;
			minutes++;
		}
		std::cout << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds << std::endl;
		Sleep(1000);
	}
	return 0;
}


The constant 0x41 is defined in this list of virtual key codes used by GetAsyncKeyState() as the letter 'a'.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

You can change it to whatever you want.
Note, that the GetAsyncKeyState() function can only be executed once every second, so the timer won't stop immediately, and will only stop if the specified key is being pressed at the exact same moment as GetAsyncKeyState() is being evaluated.
Last edited on
Thanks, it worked like a charm! Although it seems that while the timer doesn't stop immediately, it does stop regardless of when I push the button.

Final version of code for anyone who wants it:
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
32
33
34
35
36
37
38
39
40
41
#include "iostream"
#include "windows.h"
using namespace std;

int main()
{
    int a = 0; //seconds
    int b = 0; //minutes
    cout << "Press enter to start the timer, and press space to stop it.";
    cin.ignore();
    do
    {
        cout << b << ":";
        if (a < 10)
        {
            cout << "0";
        }
        cout << a << "\n";
        a++;
        if (a == 60)
        {
            a = 0;
            b++;
        }
        Sleep(1000);
    }while (!GetAsyncKeyState(0x20));//0x20 is space
    a--;
    if(a < 0)
    {
        b--;
    }
    cout << "The final time was " << b << ":";
    if (a < 10)
    {
        cout << "0";
    }
    cout << a << "\n";
    cin.ignore();//to stop it from closing immediately
    return (0);
}
Topic archived. No new replies allowed.