How to read until ESC button is pressed from cin in C++

34,940

Solution 1

int main() {
  string str = "";
  char ch;
  while ((ch = std::cin.get()) != 27) {
    str += ch;
  }

 cout << str;

return 0;
}

this takes the input into your string till it encounters Escape character

Solution 2

I found that this works for getting input for the escape key, you can also define and list other values in the while function.

#include "stdafx.h"
#include <iostream>
#include <conio.h> 

#define ESCAPE 27

int main()
{
    while (1)
    {
        int c = 0;

        switch ((c = _getch()))
        {
        case ESCAPE:
            //insert action you what
            break;
        }
    }
    return 0;
}

Solution 3

After you read the line, go though all characters you just read and look for the escape ASCII value (decimal 27).


Here's what I mean:

while (std::getline(std::cin, line) && moveOn)
{
    std::cout << line << "\n";

    // Do whatever processing you need

    // Check for ESC
    bool got_esc = false;
    for (const auto c : line)
    {
        if (c == 27)
        {
            got_esc = true;
            break;
        }
    }

    if (got_esc)
        break;
}
Share:
34,940
Katie
Author by

Katie

A girl who just loves programming :)) (but still learns ^^)

Updated on December 09, 2020

Comments

  • Katie
    Katie over 3 years

    I'm coding a program that reads data directly from user input and was wondering how could I read all data until ESC button on keyboard is pressed. I found only something like this:

    std::string line;
    while (std::getline(std::cin, line))
    {
        std::cout << line << std::endl;
    }
    

    but need to add a portable way (Linux/Windows) to catch a ESC button pressed and then break a while loop. How to do this?

    EDIT:

    I wrote this, but still - works even if I press an ESC button on my keyboard:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        const int ESC=27;
        std::string line;
        bool moveOn = true;
    
        while (std::getline(std::cin, line) && moveOn)
        {
            std::cout << line << "\n";
            for(unsigned int i = 0; i < line.length(); i++)
            {
                if(line.at(i) == ESC)
                { 
                    moveOn = false;
                    break;
    
                }
            }
        }
        return 0;
    }
    

    EDIT2:

    Guys, this soulution doesn't work too, it eats the first char from my line!

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        const int ESC=27;
        char c;
        std::string line;
        bool moveOn = true;
    
        while (std::getline(std::cin, line) && moveOn)
        {
            std::cout << line << "\n";
            c = cin.get();
            if(c == ESC)
                break;
    
        }
        return 0;
    }
    
  • Katie
    Katie over 11 years
    did like you said, could you please see my edit and say if it works for you? cheers:)
  • abnvp
    abnvp over 11 years
    @Katie : you should get one character at a time using cin.get() and break whenever you see ESC
  • Some programmer dude
    Some programmer dude over 11 years
    @Katie No, I mean you should iterate over the string line. Or read one character at a time using std::cin.get().
  • Katie
    Katie over 11 years
    changed it to: pastie.org/private/at3ujkuknnqjogqzp50gya but still something is not like I want it to be :/ it eats that character anyway