Pausing the loop till keypress

Are there anyways to pause the loop until either LCONTROL or RCONTROL is pressed?

Are there alternatives to stopping LCONTROL and RCONTROL increasing or decreasing counter by a huge sum?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

class Human{

    public:
        static int counter;

        Human(){
            counter++;
        }

        ~Human(){
            counter--;
        }
};

int Human::counter = 0;

int main()
{
    Human* h = new Human;

    bool isLeftControlPressed = false;
    bool isRightControlPressed = false;

    do{
        if(GetAsyncKeyState(VK_LCONTROL) && !isLeftControlPressed){
                h = new Human;
                isLeftControlPressed = true;
        }

        else if(!GetAsyncKeyState(VK_LCONTROL) && isLeftControlPressed){
                isLeftControlPressed = false;
        }

        if(GetAsyncKeyState(VK_RCONTROL) && !isRightControlPressed){
                delete h;
                isRightControlPressed = true;
        }

        else if(!GetAsyncKeyState(VK_RCONTROL) && isRightControlPressed){

                isRightControlPressed = false;
        }
        cout<<"Human Population : "<<Human::counter<<endl;

    }while(Human::counter > 0 && Human::counter < 10);

    if(Human::counter == 0){
        cout<<"\n\nWahahah No Human is alive!!\n";
    }

    else if(Human::counter == 10){
        cout<<"\n\nHumans Will Dominate!!\n";
    }

    return 0;
}


Last edited on
ReadConsoleInput() is probably the right choice here. It is a bit more complex, so make sure to read MSDN article and google for code examples.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961%28v=vs.85%29.aspx
What about the infinite printing of text? is there a way to print the text once then wait for input only printing a second time?
Make sure text is not displayed at each passing through the loop and only once per button press. Add bool alreadyPrinted = false; alongside other bools. Then change line 49 cout<<"Human Population : "<<Human::counter<<endl; to:

1
2
3
4
5
6
7
8
9
10
if (isRightControlPressed == false && isLeftControlPressed == false && 
    alreadyPrinted == true)
    alreadyPrinted = false;

if ((isRightControlPressed == true || isLeftControlPressed == true) 
    && alreadyPrinted == false)
{
    cout<<"Human Population : "<<Human::counter<<endl;
    alreadyPrinted = true;
}


It worked brilliantly. Thanks, but i dont really get how to use the ReadConsoleInput() do you have any good examples?
Especially for you :D - a bit lenghty example I recovered from my old projects. It should compile as it is:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

void gotoxy( int x, int y )
{
    COORD c;
    c.X = x - 1;
    c.Y = y - 1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main()
{

    cout << "\t\t\tPress LEFT ARROW or RIGHT ARROW. "
        << "\n\t\t\tTry typing some very long text. "
        << "\n\t\t\t\tPress ENTER to exit";

    // == These are needed for this input box in the middle, ignore them for now == //
    COORD prompt = {35, 11};
    gotoxy(prompt.X, prompt.Y);
    cout << "> ";
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CursorOn = { 100, true };
    SetConsoleCursorInfo(hConsole, &CursorOn); // Just makes cursor big
    int width = 10;
    char buffer[1] = "";
    string userInput = "";
    // ======================================================

    // This is relevant to you:
    // The key element to using ReadConsoleInput is this structure described here:
    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682079%28v=vs.85%29.aspx
    // This is where input events go to. Usually it's bigger than 1, 
    // but for simple console program one will do (need more if you want handle mouse too)
    INPUT_RECORD input[1];		
									
    // ReadConsoleInput returns number of events here, not very important for us
    DWORD NumberOfEventsRead;	

    // Think of it as of a pointer to Keyboard ;)
    HANDLE hKeyboard = GetStdHandle(STD_INPUT_HANDLE);

    while ( true )
    {
        ReadConsoleInput(hKeyboard, input, 1, &NumberOfEventsRead);
        if ( input[0].EventType == KEY_EVENT 
             && input[0].Event.KeyEvent.bKeyDown ) // If key was pressed
        {
            if (input[0].Event.KeyEvent.wVirtualKeyCode == VK_LEFT) 
            {
                gotoxy(1, 10);
                cout << "<=== HERE";
                gotoxy (70, 10);
                cout << "          ";
            }

            if (input[0].Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
            {
                gotoxy(70, 10);
                cout << "THERE ===>";
                gotoxy (1, 10);
                cout << "         ";
            }
			
            /* 
            Do not get into the rest of the code for now...
            I should really use sth like
					
                    swith (input[0].Event.KeyEvent.wVirtualKeyCode) 
                    {
                        case VK_LEFT:
                        case VK_RIGHT:
                        etc..
                    }
			
             here, but it's old, and I don't feel like
             rewriting it and messing with working code
             */

            string::iterator end = userInput.end(); // Retrieve the character that was pressed.
            buffer[0] = input[0].Event.KeyEvent.uChar.AsciiChar;
            if ( buffer[0] == 13 ) // enter pressed
            { 
                gotoxy(prompt.X, prompt.Y);
                for (size_t i = 0; i < width; i++)
                    cout << ' ';
                gotoxy(prompt.X, prompt.Y); // cleanup
                break;
            }

            if ( buffer[0] == 8 ) // backspace
            { 
                if ( userInput.size() > 0 )
                    userInput.erase(end - 1);
            }
			
			// 0 is function keys, shift, ctrl, etc, 27 is Esc '<-', 09 is Tab "->"
            else if ( buffer[0] != 0 && buffer[0] != 27 && buffer[0] != '\t' )
            { 
                userInput += buffer[0];
            }

            gotoxy(prompt.X, prompt.Y);
            cout << "> ";
            if ( static_cast<short>(userInput.size()) < width - 2 )
            {
                cout << userInput;
                for (size_t i = userInput.size() + 2; i < width; i++) //black-out rest of the line
                    cout << ' ';
                gotoxy(prompt.X + userInput.size() + 2, prompt.Y);
            }

            else
            {
                cout
                << userInput.substr(userInput.size() - width + 2,
                                    userInput.size());
                gotoxy(prompt.X + width - 1, prompt.Y);
            }
        }
    }

	gotoxy (1, 23);
	cout << "You have written: " << userInput << endl << endl; 
	system ("pause");
}
Thanks. Will give it a try to understand, thanks for the help!
Topic archived. No new replies allowed.