【问题标题】:How can I write a command line shell to capture 'UP' key to get historic input?如何编写命令行 shell 来捕获“UP”键以获取历史输入?
【发布时间】:2013-09-01 18:23:28
【问题描述】:

我想为一个后台程序编写一个命令行 shell,我可以在其中输入一些命令。我想添加一个功能,比如 bash(或其他类似 shell)---'UP' 键来获取历史输入。如何捕获按下的“UP”键?

如果我只使用std::getline(std::cin, line),我无法获得“UP”键或其他功能键,即。 Ctrl+w 删除输入行的一个单词。

【问题讨论】:

标签: c++ c shell command-line


【解决方案1】:

有一个readline功能,支持历史和行编辑等。

在基本模式下,它会读取行等。如果你想添加钩子,你可以在按下tab或类似的时候让它展开命令。

这就是你在 Linux 中典型的 shell 所使用的。

这里的文档: http://web.mit.edu/gnu/doc/html/rlman_2.html

这里有一个例子: http://www.delorie.com/gnu/docs/readline/rlman_48.html

主项目页面: http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html

【讨论】:

    【解决方案2】:

    使用 ncurses 库,见sample program:

    #include<ncurses.h>
    #include<iostream>
    
    int main()
    {
        int ch;
        initscr();  //initialize the ncurses data structures
    
        keypad(stdscr,TRUE); //enable special keys capturing
    
        ch=getch();
    
        if(ch==KEY_UP)
            std::cout << "Key up pressed!" << std::endl;
    }
    

    【讨论】:

      【解决方案3】:

      使用kbhit() 获取键盘方向键

      【讨论】:

      • 它适用于所有编译器吗? see
      猜你喜欢
      • 2013-08-21
      • 2016-08-05
      • 1970-01-01
      • 2013-05-12
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多