tom
tom

Reputation: 507

Console get key press w/o windows messages c++

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.

Upvotes: 1

Views: 2436

Answers (3)

jmucchiello
jmucchiello

Reputation: 18984

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.

Upvotes: 0

Elemental
Elemental

Reputation: 7476

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?

Upvotes: 0

ChrisW
ChrisW

Reputation: 56113

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

Upvotes: 1

Related Questions