0

I'm trying to simulate the history of a shell like script. The first step (at least from what I think) is to make it recognise the up or down arrows. Let me show you the code first:

string readSequence() {
    int c;
    string currLine;
    do {
       if (getchar() == '\033') { // if the first value is esc
            getchar(); // skip the [
            c = getchar(); // get the real value
                if(c == 'A') // the real value
                    cout << "up";
                if(c == 'B')
                    cout << "down";
        }
    } while(c != EOF || c != '\n');
}

Whenever I run this in the command line, if I press up down up down for instance, it will print ^[[A^[[B^[[A^[[B, and after I press enter it will print updownupdown. I'm trying to make it print up or down as soon as the user presses up or down, and also make it not to print those ^[[A or ^[[B things.

Also, if you got any suggestions on how to do this in another way, please do share. Thanks in advance!

Toby
  • 9,696
  • 16
  • 68
  • 132
  • maybe this helps http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – cokceken Nov 25 '16 at 11:30
  • Is there a way to do this without any external libraries? – Emanuel Pop Nov 25 '16 at 11:34
  • Is this C *or* C++? Please use *only* the appropriate tag as otherwise both communities will likely be peeved at you. – Toby Nov 25 '16 at 12:02
  • Possible dupplicate of http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar – Toby Nov 25 '16 at 12:07
  • The way to do this without an external library is to reimplement (some portions of) that library. I would not recommend it if you have a choice. – n. m. could be an AI Nov 25 '16 at 12:57

0 Answers0