views:

919

answers:

3

Is there any way to get the last key press in a console without using Windows messages or the std::cin stream? I've heard that there is a function in the standard library. Solutions should preferably be as portable as possible. Thanks for your help in advance.

+1  A: 

There's conio.h but it's not technically standard. On Linux, my first Google hit suggests termios.h.

ChrisW
A: 

Not really portable but you can access the current key state using GetAsyncKeyState even from console app under windows. More technical, and equally windows specific, would be to hook the keyboard using SetWindowsHookEx into a call back in your system that simply stores the last key pressed.

But your basic problem: Console + Portability - seems to imply cin is your best bet - what do you need that cin doesn't provide?

Elemental
A: 

Have you considered using a curses library like pdcurses? That's about the only cross-platform library that will do console management that I know of.

jmucchiello