5

Possible Duplicate:
C++ console keyboard events

I want a Windows console program to do something if a certain key is pressed down, something like

while(1)
{
    ....
    if(the key 'o' is pressed down)
      ....
}

but I don't know what to put in the if statement. How do I check if the key 'o' is pressed down?

I'm using Windows 7 64-bit and Visual Studio Professional 2008.

Community
  • 1
  • 1
Timothy
  • 51
  • 1
  • 1
  • 3
  • 13
    You are missing the critical information: your environment, operating system, GUI libraries you are using or intend to use, etc. – Juliano Jun 25 '11 at 17:10
  • Which operating system are you using? – Scott Langham Jun 25 '11 at 17:29
  • Sorry, I'm using Windows 7 64-bit – Timothy Jun 25 '11 at 17:35
  • 4
    Are you writing a console program or a Win32 window application? – Xeo Jun 25 '11 at 17:38
  • I think it's likely that the person was asking for the non-blocking equivalent to `cin >> c;` as a non-platform specific solution. That's not a duplicate of a question asking something specifically about a windows console application. I got here from Google because I'm looking for the same solution, but I'm working in Linux. Finding this question shut down is very disappointing. – Jherico Aug 02 '13 at 20:21
  • This question is not a duplicate. The other question refers specifically to *key events*, but the simplest solution to this question is not to use *key events*, but to check the *key state*. – Johan Råde Jul 30 '14 at 06:36
  • See also: getch (http://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key) – Andrew Feb 18 '17 at 16:07

2 Answers2

1

You can use the std::cin.get() or you can use the windows.h GetAsyncKeyState, depending on what exactly you want to do.

If you want lower level stuff, look into hooks and events from the WinAPI.

ultifinitus
  • 1,813
  • 2
  • 19
  • 32
0

Rather than busy-polling for a keypress, you should register for key events in your application (assuming this is a Windows GUI app), and check for the keys you're interested in.

If you are actually making a console app, see here: C++ console keyboard events

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • This doesn't really answer the question properly. There is a standard C++ way of waiting on input from stdin, but this question is asking for an equivalent mechanism that doesn't block. Neither suggesting that he change the structure of his program, or use a platform specific API (even if, in this case it's the platform he's actually on) is actually helpful. I found this question hoping to get an answer that was C++ specific, not windows specific and instead find a question that has been improperly closed as a dupe and never properly answered. – Jherico Aug 02 '13 at 20:19
  • There is no standard C++ way of waiting for input that does not block. The only ways to do it are either platform-specific or depend on a separate standard like POSIX (e.g. using `select()` to wait on events on `STDIN_FILENO`). – John Zwinck Aug 03 '13 at 02:37
  • That's pretty much what I figured. The problem is that this shouldn't be in a comment, it should be the accepted answer on a question that isn't closed as a duplicate. – Jherico Aug 03 '13 at 16:24
  • You are welcome to submit a new question with your own answer included. I hope you'll understand my reticence to do anything with this particular question though, since it's two years old and not really wrong. If you do submit a new one, please do link it here so future visitors can follow along. – John Zwinck Aug 04 '13 at 11:04