使用 C 语言的自定义 shell 中的箭头键控制

Bas*_*ous 2 c shell getchar arrow-keys

我正在尝试使箭头键在单行(左+右)中的字符之间以及自定义外壳(学期项目)的历史命令(上+下)之间移动。

此时,当击中其中一个箭头时 ^[[A, ^[[B, ^[[C 或 ^[[D 显示出来,按下回车后,我意识到其中一个被击中了:

char a = getchar();

if (a == '\033') {
    getchar();
    int ch2 = getchar();
    switch(ch2){
        case 'A': 
            printf("UP\n"); 
            break;
        case 'B': 
            printf("DOWN\n"); 
            break;
        case 'D': 
            printf("LEFT\n"); 
            break;
        case 'C': 
            printf("RIGHT\n"); 
            break;
        default:
            printf("SOME OTHER SCROLL KEY PRESSED: %d %d\n", a, ch2); 
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想得到的是,一旦我击中其中一个箭头,动作就会发生而不会显示任何内容。

Cer*_*ach 5

默认情况下,unix 系统中的终端输入是行缓冲的,您可以使用 termios 为 stdin 函数指定自己的返回条件:

#include <stdio.h>
#include <termios.h>

static struct termios orig_termios;

char get_char_wait_for_keypress(void) {
    struct termios raw;
    // Get stdin file descriptor (0 by default)
    int stdin_fileno = fileno(stdin);
    // Copy terminal io settings
    raw = orig_termios;
    // Set return condition at first byte being received (For input timeout you can use `raw.c_cc[VTIME] = secs`)
    raw.c_cc[VMIN] = 1;
    // Apply settings with new return condition
    tcsetattr(stdin_fileno, TCSANOW, &raw);
    // Get char with new conditions
    char c = getchar();
    // Restore old settings
    tcsetattr(stdin_fileno, TCSANOW, &orig_termios);
    return c;
}

int main(void) {
struct termios raw;
    char c = get_char_wait_for_keypress();
    printf("%d", c);
}
Run Code Online (Sandbox Code Playgroud)