How to pause keyboard input in C++ (cin input)

I have just learnt about the funciton GetKeyState() from windows.h, and I want to use it in a program but I have this problem: after I use GetKeyState() the letters I pressed including Enter will be automatically typed when I use cin>> or getline, you will understand better from this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    while(GetKeyState('1')>=0){}//Wait until 1 is pressed (you can press whatever you want without 1 and nothing will happen)
    string s;
    getline(cin,s);//All the letters you pressed before will be automatically typed here including 1 at the end
    cout<<s;

    return 0;
}
Last edited on
try
string s;
while(GetKeyState('1')>=0){getline(cin,s)}//Wait until 1 is pressed (you can press whatever you want without 1 and nothing will happen)
getline(cin,s)
etc

not an expert on windows, but its probably a hardware trap, and the normal flow to your cin buffer is still running as usual, so you can just read it and discard it until you get what you want. if getline isn't correct, read it some other way or do some sort of empty command to clear it out.
Last edited on
@jonnin Thanks for your response! But it doesn't really work, plus I don't want to see anything until I press 1. I wish I could use a function to do that, I searched it on Google and I found cin.ignore() but I can't make it to work. For example this: cin.ignore(1000,'1') should ignore 1000 characters until 1 is pressed, but you can still see the characters on the screen, and you have to press Enter so '1' is considered pressed. Anyway I don't think cin.ignore() is the solution but I am looking for sth like that, for example cin.start_ignoring and cin.stop_ignoring which stops/start the keyboard input (I am refering here to the cin input, I mean if you press '1' it won't appear on screen and won't be saved anywhere) would be the function I want. Or sth like cin.ignore_previous_input().
I found the "cin.ignore_previous_input()" function on another forum:
1
2
3
4
5
6
7
8
void ClearConsoleInputBuffer()
{
    PINPUT_RECORD ClearingVar1 = new INPUT_RECORD[256];
    DWORD ClearingVar2;
    ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),ClearingVar1,256,&ClearingVar2);
    delete[] ClearingVar1;
}
Edit: and btw this is the code for what I wanted to do up there:
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
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void ClearConsoleInputBuffer()
{
    PINPUT_RECORD ClearingVar1 = new INPUT_RECORD[256];
    DWORD ClearingVar2;
    ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),ClearingVar1,256,&ClearingVar2);
    delete[] ClearingVar1;
}

int main()
{
    bool key=false,key0=false;
    while(true)
    {
        key=(GetKeyState('1')<0);
        if(key0 && !key)///I did this so the program starts reading s after you release 1 otherwise it would take 1 as input for the string too unless you press it super quick
            break;
        key0=key;
    }
    ClearConsoleInputBuffer();
    string s;
    getline(cin,s);
    cout<<s;
}
Last edited on
I think you're over-complicating things if you're using console applications you can use system("pause") to pause the program.

I may be wrong but this does what you asked.
Last edited on
System("pause") is a bad practice.
It's platform specific as well as slow and inefficient; and doesn't really help you learn anything.
Last edited on
Topic archived. No new replies allowed.