【问题标题】:Detecting arrow keys via the standard input stream on LINUX通过 LINUX 上的标准输入流检测箭头键
【发布时间】: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 的解决方案。 有什么想法吗?

【问题讨论】:

标签: c++ linux arrow-keys


【解决方案1】:

使用这个:ncurses 谷歌一些简单的例子如何检测箭头

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多