Question

Such as if the user is about to enter something using the cin function, but after a while it forces the user to press enter without them actually pressing enter so it ends the cin function.

  1. Cin is waiting for user to finish.
  2. Another thread calls a bool value that says cin must end.
  3. Cin needs to enter itself as it is even if the user did not finish typing.
Was it helpful?

Solution

The functionality of the enter key is implemented by the shell program, and the C++ standard library does not define any control over it. You need to send some interprocess signal to get the shell, or whatever is outputting to cin (on Unix, identified by file descriptor 1), to flush its output.

The usual way to provide an interactive text interface on a Posix system is ncurses, but I haven't seen it used with iostreams. (Well, I'm not really familiar with curses at all.)

OTHER TIPS

I did some digging around and found a way to do it!

#include <windows.h>

INPUT input;
memset(&input,0,sizeof(input));
input.type = INPUT_KEYBOARD;
input.ki.wVk=VkKeyScanA('\n');
SendInput(1,&input,sizeof(INPUT));
input.ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(1,&input,sizeof(INPUT));

It stimulates the enter key, or a new line.

As seen here http://www.dreamincode.net/forums/topic/153423-simulate-keyboard-press/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top