0

我需要让我的while循环工作,直到按下键盘上的 ESC 按钮。

char choose = NULL;
while( choose != 27)
{
    cout << "Choose (s), (e) or (n): ";
    cin.ignore();
    choose = cin.get();

    switch(choose){
    case 's': {SortRoutesByStartPoint(routeList, n); ShowRoutes(routeList, n, "Sorted list (s):"); break;}
    case 'e': {SortRoutesByEndPoint(routeList, n); ShowRoutes(routeList, n, "Sorted list (e):"); break;}
    case 'n': {SortRoutesByNumber(routeList, n); ShowRoutes(routeList, n, "Sorted list (n):"); break;}
    default: {cout << "Not found\n\n"; break;}
    }
}

但是当我按下 ESC 按钮时,它什么也没有发生。为什么?
如何让它发挥作用?

4

2 回答 2

1

简单的答案是您不能,至少不能可靠地使用标准流。如果你从终端输入,你会得到操作系统给你的东西。通常,在按下回车之前它不会给你任何东西,并且在此之前会删除很多字符以进行行编辑和其他事情;ESC 在这些字符中有一个明显的变化。

当您想逐个字符阅读时(就像您显然所做的那样),您需要一些第三方库,例如 curses。或者您必须自己编写大量系统相关代码。

于 2013-10-16T13:24:37.857 回答
0
#include <conio.h>

cout << "Choose (s), (e) or (n): ";
// cin.ignore();
// choose = cin.get();

choose =getche();
if(choice==0)  //an extended character code is next
  choice=getche();
于 2013-10-16T14:04:34.857 回答