How to handle key press events in C ++

I am writing a special console program. And I want it to look like a real one. Therefore, I want to associate some actions with keystroke events.

For example, when the up arrow is pressed, previously executed commands should be displayed to the user. I know about SDL. But I think this is not a standard library, right?

If there is another alternative included in the standard CPP library, let me know.

Thanks.

+4
source share
2 answers

You will not find anything in the standard library for this. It all depends on the platform. On Windows, you have functions like GetAsyncKeyState to get the state of a key on a keyboard, for example.

SDL and SFML both have platform independent event handling.

+5
source

What you are describing is not a "console program" as such, but a shell. In addition, you do not want to handle incoming events, you just want to just read from the command line.

There are various ways to do this. Windows has a ReadConsoleInput . A more flexible way, however, is that this is one of the getline values.

 int main () { string mystr; cout << "What your name? "; getline (cin, mystr); return 0; } 

To make you special work, you just need to keep the previous entries of std::vector<string> or similar.

To read the original input (without echo) from the console, you should use _ getch ()

+1
source

Source: https://habr.com/ru/post/1490019/


All Articles