7

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?

Is there is other alternative of it, that is included in the standard C++library?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Bhaira
  • 1,687
  • 6
  • 18
  • 31
  • 2
    What's your OS ? And choose C or C++, don't tag the both. – nouney Jul 06 '13 at 08:13
  • You can't do this with *just* the standard library (`c` or `c++`). You need some other library or os utilities. You can use the [readline](http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html) library for handling input history on Linux, for instance. – BoBTFish Jul 06 '13 at 08:13
  • This is OS dependant. – huseyin tugrul buyukisik Jul 06 '13 at 08:13
  • 1
    @nouney it's Ubuntu 12.x. and If you can do something in c then obviously you can do it in c++ also. – Amit Bhaira Jul 06 '13 at 08:41
  • 1
    Yes, and anything you can do in C and C++, you can also do in assembler. That doesn't mean you should add that tag also. The point is that you do things very *differently* in C and C++ since they're *different* programming languages. Therefore, the answers you get will be different, depending on which one you're using. We figure that out based on the tags. – Cody Gray - on strike Jul 06 '13 at 09:06
  • You can take look at bash source code too - After all you have working code, though in `C`. – VoidPointer Jul 06 '13 at 09:09
  • 1
    @CodyGray I am using both c and c++. It's a big project, some section we are writing in c and some in c++. And if I will get a library implemented in any of these language, I can integrate it. And this is my problem. I have added more number tags so that I can get more number of answers. And people who knew it, answered it without preaching. – Amit Bhaira Jul 06 '13 at 11:45
  • How is a console program if it is "an actual one"? – HelloGoodbye Sep 25 '15 at 08:41

2 Answers2

8

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.

SDL and SFML both have platform-independent event handling.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user123
  • 8,970
  • 2
  • 31
  • 52
  • No. He'd have to handle that on his own. These would only be used for input handling, managing the window and rendering the text (Assuming he's making a completely custom console from scratch, independent of the OS console). – user123 Jul 06 '13 at 08:23
  • it's ok I will do that .. so I should go ahead with SDL. – Amit Bhaira Jul 06 '13 at 08:40
  • 1
    It depends on what you're most comfortable with. SDL has a C API (functions, structs, pointers everywhere) while SFML has a C++ API (classes, usage of references instead of pointers, etc...) – user123 Jul 06 '13 at 08:45
  • 1
    @Magtheridon96 Actually I am planning to have two threads in this scenario, first thread will be blocked in getline(cin,buffer) and another will keep listening for key press. As soon as the user will hit any of (up or down) key it will print a previously used command, moreover it will fill the buffer(doubt here, cin can do it or not ?? In this scenario) of first thread. And when the user will hit enter first thread will wake up and do it's job. Will it be a good approach? Or can you suggest me a better one ?? – Amit Bhaira Jul 06 '13 at 12:01
  • That could work well. I can't think of many other approaches to the problem. – user123 Jul 06 '13 at 18:27
-2

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 using 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()

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 8
    I agree `getline` is the best way to handle simple reading, but it doesn't do what the question asks. – BoBTFish Jul 06 '13 at 08:15