I am writing a custom console program. And I want to make it look like an actual one. So I want to bind some actions with keypress events.
For example when the up arrow is pressed previously executed commands should be shown to the user. I know about SDL. But I think that it's not a standard library, is it ??
If there is other alternative of it, that included in standard CPP library, please let me know.
Thanks.
You won't find anything in the standard library for that. It's all Platform-dependent. In Windows, you have functions like GetAsyncKeyState
to get the state of a key on the keyboard for example.
What you describe is not a "console program" per se, but a shell. Also, you don't want to handle incoming events, you rather want to simply read from the command line.
To do this, there are various ways. Windows has ReadConsoleInput. The more flexible way though is this one usign getline.
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
return 0;
}
To make you special thing working you just got to store previous inputs in a std::vector<string>
or similar.
To read the raw input (without echo) from the console, you should use _getch()
来源:https://stackoverflow.com/questions/17501014/how-to-handle-key-press-events-in-c