3

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

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

4

3 回答 3

4

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

于 2013-08-29T12:30:36.713 回答
0

用于kbhit()获取键盘箭头键

于 2013-08-29T10:59:53.433 回答
0

使用 ncurses 库,参见示例程序

#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;
}
于 2013-08-29T11:00:30.193 回答