C++ 如何在C++中从cin按下ESC按钮之前读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14214753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read until ESC button is pressed from cin in C++
提问by Katie
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:
我正在编写一个直接从用户输入读取数据的程序,我想知道如何在按下键盘上的 ESC 按钮之前读取所有数据。我只找到了这样的东西:
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?
但需要添加一种可移植的方式(Linux/Windows)来捕捉按下的 ESC 按钮,然后中断 while 循环。这该怎么做?
EDIT:
编辑:
I wrote this, but still - works even if I press an ESC button on my keyboard:
我写了这个,但仍然 - 即使我按下键盘上的 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:
编辑2:
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;
}
回答by abnvp
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
这会将输入带入您的字符串,直到遇到转义字符
回答by Some programmer dude
After you read the line, go though all characters you just read and look for the escape ASCII value (decimal 27).
阅读该行后,查看您刚刚阅读的所有字符并查找转义 ASCII 值(十进制 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;
}
回答by Michael Wolff
I found that this works for getting input for the escape key, you can also define and list other values in the while function.
我发现这适用于获取转义键的输入,您还可以在 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;
}
回答by AmmAr
#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;
}
回答by Paulo
I would suggest that for not just ESC character in C++, but for any other character of the keyboard in any language, read characters that you input into an integer variable and then print them as integer.
我建议不仅对于 C++ 中的 ESC 字符,而且对于任何语言中键盘的任何其他字符,读取您输入到整数变量中的字符,然后将它们打印为整数。
Either that or search online for a list of the ASCII characters.
或者在线搜索 ASCII 字符列表。
This will give you ASCII value of the key, and then it's plain simple
这将为您提供密钥的 ASCII 值,然后就很简单了
if(foo==ASCIIval)
break;
For the ESC character, the ASCII value is 27.
对于 ESC 字符,ASCII 值为 27。