see if Esc key is pressed while awaiting input from user

I have a function that currently waits for input from the user and stores it into a long unsigned int. My problem is I want to make it so that if the user presses the Esc key, it does something. Is there a way to do that? I know the escape key is 27.. but thats it
you can use getasynckeystate to accomplish this:

1
2
3
4
5
if (GetAsyncKeyState(VK_ESCAPE)) {
			//do something
			cin.ignore();
		}
	}
Last edited on
That won't work if he's waiting in an iostream command (ie cin >> var;), as his program will not get any cpu time until the user actually inputs something and presses enter.

The "easiest" way to do this in the console is probably to not use iostream, but instead get individual keypresses and manually build your own buffered string.

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string input;
while(true)
{
  int foo = getch(); // note:  getch() used here only as an example, may not be a real function

  if( foo == escape )  // again, escape is only an example -- not real
  {
    // do whatever you want to do on escape
  }
  else if(  foo is alphanumeric )
  {
    input += char(foo);
  }
  else if( foo == enter )  // again, example
  {
    // use 'input' as the full user input here
  }
}


As you can see, it's not exactly the most straightforward thing in the world.

Really, you probably shouldn't even be trying to do it. Whenever people try to twist the console into doing weird things I always have the urge to ask why.

So why are you trying to do this?
Topic archived. No new replies allowed.