Clear user input before it is entered

I am working on a Win32 console application that uses a keylogger to determine when a specific key sequence is pressed and then opens a console window when it is. When the window is made visible, it prompts the user to enter a string of text. Unfortunately, everything the user has typed since the program started running is already there.
For example, if I opened the program, typed "ABC" in a notepad application, and then performed the key sequence, the window would open up and the cursor would be pre-filled with "ABC."
How can I stop this from happening? I have tried using cin.clear() and similar methods, but to no avail.
A basic outline of the code is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  while(true)
  {
    if(GetAsyncKeyState(/* specific key */) == -32768 /* && ... */)
    {
      std::string str = "";
      while(str != "EXIT") {
        std::cin >> str; // Screen is already filled with characters.
        /* ... */
      }
    }
  }
}

Last edited on
Solved it, here's my solution:
(before the inner while loop)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	std::string command = "__NOT_EMPTY__"; // Can be anything except ""
	char* cmd_raw = new char[MAX_MESSAGE_LENGTH];
	while (command != "") {
		INPUT inp;
		inp.type = INPUT_KEYBOARD;
		KEYBDINPUT kbdi;
		kbdi.wVk = VK_RETURN;
		kbdi.wScan = NULL;
		kbdi.dwFlags = NULL;
		kbdi.dwExtraInfo = NULL;
		kbdi.time = GetTickCount() + 10;
		inp.ki = kbdi;
		SendInput(1, &inp, sizeof(inp));
		std::cin.getline(cmd_raw, MAX_MESSAGE_LENGTH);
		command.assign(cmd_raw);
		std::cin.clear();
	}
	std::cin.getline(cmd_raw, MAX_MESSAGE_LENGTH);
	system("CLS");
Topic archived. No new replies allowed.