GitHub如何使用termios區分Escape和Up/Down/Left/Right?
這是最好的我可以拿出來處理ncurses風格的按鍵(我實際上是由於各種原因寫一個替代ncurses)。
使用此代碼構建的示例應用程序建議用戶「通過按Escape退出」。事實上,它需要Escape + Escape或Escape +一個箭頭鍵。我想解決這個問題。
#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";
}
你介意鏈接到確切的代碼,我不知道在哪裏查看您的代碼庫。 – JustGage