【发布时间】:2011-07-14 10:17:32
【问题描述】:
这是我能想到的最好的处理 ncurses 式按键的方法(出于各种原因,我实际上正在编写 ncurses 的替代方案)。
使用此代码构建的示例应用建议用户“按 Escape 退出”。事实上,它需要 Escape + Escape 或 Escape + An Arrow Key。我想解决这个问题。
#include <sys/ioctl.h>
#include <termios.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_key() {
char c = getchar();
switch(c) {
case 'a': return "a";
case 'b': return "b";
case 'c': return "c";
...
case '\x1b':
c = getchar();
switch(c) {
case '[':
c = getchar();
switch(c) {
case 'A': return "up";
case 'B': return "down";
case 'C': return "right";
case 'D': return "left";
}
case '\x1b': return "escape";
}
default: return "unknown";
}
【问题讨论】:
标签: escaping ncurses arrow-keys termios