【发布时间】:2015-12-08 07:34:26
【问题描述】:
我正在尝试为我的工具实现命令行,我希望用户可以通过按终端中的向上/向下箭头键轻松获取上一个命令。
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char** argv){
std::vector<std::string> stack;
std::string buffer;
std::string str;
while(1){
buffer.clear();
str.clear();
std::cout<<"cmd>";
while(std::cin.peek() != '\n'){
std::cin >> buffer;
if(!str.empty()){
str += ' ';
}
str += buffer;
}
if(str == "quit"){
break;
}
else{
stack.push_back(str);
}
std::cout<<"Stack: ";
for(int i = 0;i < stack.size();i++){
std::cout<<stack[i]<<"->";
}
std::cout<<std::endl;
std::cin.clear();
std::cin.ignore();
}
return 0;
}
到目前为止,它看起来像这样,但我不知道如何检测箭头键。我知道没有标准的 C++ 方法可以做到这一点,我可以接受仅限 LINUX 的解决方案。 有什么想法吗?
【问题讨论】:
-
强烈建议:考虑使用ncurses
-
使用标准 C++ 功能和标准
std::cin流并不能很好地完成。根据平台,您可能希望查看诸如 the GNU readline library 或 the Windows console functions 之类的内容。
标签: c++ linux arrow-keys