我正在尝试模拟像脚本一样的shell的历史。第一步(至少从我的想法)是让它识别向上或向下箭头。我先告诉你代码:
string readSequence() {
int c;
string currLine;
do {
if (getchar() == '\033') { // if the first value is esc
getchar(); // skip the [
c = getchar(); // get the real value
if(c == 'A') // the real value
cout << "up";
if(c == 'B')
cout << "down";
}
} while(c != EOF || c != '\n');
}
每当我在命令行中运行它时,如果我向上按下向上,它将打印^[[A^[[B^[[A^[[B
,然后按Enter键后将打印updownupdown
。我试图在用户按下或按下时立即打印up
或down
,并且不打印那些^[[A
或^[[B
内容。
此外,如果您对如何以其他方式执行此操作有任何建议,请分享。提前谢谢!