【问题标题】:How to read until ESC button is pressed from cin in C++如何读取直到从 C++ 中的 cin 按下 ESC 按钮
【发布时间】:2012-12-22 07:00:48
【问题描述】:

我正在编写一个直接从用户输入读取数据的程序,我想知道在按下键盘上的 ESC 按钮之前如何读取所有数据。我发现只有这样的东西:

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

但需要添加一种可移植的方式(Linux/Windows)来捕捉按下的 ESC 按钮,然后中断 while 循环。如何做到这一点?

编辑:

我写了这个,但即使我按下键盘上的 ESC 按钮仍然可以工作:

#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:

伙计们,这个解决方案也不起作用,它吃掉了我的第一个字符!

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

【问题讨论】:

  • 你可以设置一个const int ESC=27;,然后在你的循环中使用c=getch(),然后检查c是否等于ESC来终止。
  • @MarounMaroun: 不是 getch();只有 Windows 解决方案...?
  • 使用 cin.get() 代替 getch()
  • @abhinav:不起作用,请参阅我的编辑
  • @Katie:只要把角色放回行中。

标签: c++ escaping getline


【解决方案1】:
int main() {
  string str = "";
  char ch;
  while ((ch = std::cin.get()) != 27) {
    str += ch;
  }

 cout << str;

return 0;
}

这会将输入带到您的字符串中,直到遇到转义字符

【讨论】:

    【解决方案2】:

    读完这一行后,检查你刚刚读到的所有字符并查找转义 ASCII 值(十进制 27)。


    这就是我的意思:

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

    【讨论】:

    • 像你说的那样,你能看看我的编辑并说它是否适合你吗?干杯:)
    • @Katie :您应该使用 cin.get() 一次获取一个字符,并在看到 ESC 时中断
    • @Katie 不,我的意思是你应该遍历字符串line或者使用std::cin.get()一次读取一个字符。
    • 已将其更改为:pastie.org/private/at3ujkuknnqjogqzp50gya,但仍然有些东西不像我想要的那样:/它无论如何都会吃掉那个角色
    【解决方案3】:

    我发现这适用于获取转义键的输入,您还可以在 while 函数中定义和列出其他值。

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

    【讨论】:

      【解决方案4】:
      #include <iostream>
      #include <conio.h>
      
      using namespace std;
      
      int main()
      {
          int number;
          char ch;
      
          bool loop=false;
          while(loop==false)
          {  cin>>number;
             cout<<number;
             cout<<"press enter to continue, escape to end"<<endl;
             ch=getch();
             if(ch==27)
             loop=true;
          }
          cout<<"loop terminated"<<endl;
          return 0;
      }
      

      【讨论】:

        【解决方案5】:

        我建议不仅对于 C++ 中的 ESC 字符,而且对于任何语言中的任何其他键盘字符,读取您输入到整数变量中的字符,然后将它们打印为整数。

        或者在线搜索 ASCII 字符列表。

        这将为您提供密钥的 ASCII 值,然后就很简单了

        if(foo==ASCIIval)
           break;
        

        对于 ESC 字符,ASCII 值为 27。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多