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

前端 未结 5 1237
予麋鹿
予麋鹿 2020-12-10 19:17

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 li

5条回答
  •  情书的邮戳
    2020-12-10 19:54

    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;
    }
    

提交回复
热议问题