getchar()的怪异行为

发布于 2025-01-25 04:12:24 字数 1163 浏览 4 评论 0原文

目前,我正在使用C语言进行一个项目,其中我必须编写一段时间循环才能不断接收键盘输入或操纵杆输入。操纵杆输入还可以,但是对于键盘输入,有两种输入类型:箭头和正常输入('a','b',...)。对于键盘输入,我引用了此链接( https://stackoverflow.com/a/11432632 )以接收箭头键。 这是我的代码:

while(true){
    if (getchar() == '\033') { // if the first value is esc
        getchar(); // skip the [
        switch(getchar()) { // the real value
        case 'A':
            printf("arrow up \n");
            break;
        case 'B':
            printf("arrow down \n");
            break;
        case 'C':
            printf("arrow right \n");
            break;
        case 'D':
            printf("arrow left \n");
            break;
        }
    }
    if (getchar() != '\033'){
        printf("non arrow \n");
    }
}

但是,即使我按下了箭头按钮,也会不断出现“非箭头”。

如果我通过具有变量char c从printf(“ non Arrow \ n”)更改了代码,请将getchar()分配给它,然后将getchar()和以后的打印c:

    if (getchar() != '\033'){
                printf("non arrow \n");
            }

输出量为箭头密钥的预期(接收和打印符合预期),但是输入“ E”或“ R”或其他单个字符键时,什么都不会出现。

我想知道我的代码问题是什么,如何将其修改以按照我的意愿接收行为。

我希望尽快得到答案。

谢谢你,恩吉恩。

Currently, I am doing a project in c language in which I have to write a while loop to constant receive either keyboard input or joystick input. The joystick input is okay but for the keyboard input, there are 2 types of inputs: arrow and normal input ('a', 'b', ...). For the keyboard input, I referred to this link (https://stackoverflow.com/a/11432632) to receive arrow keys.
Here is my code:

while(true){
    if (getchar() == '\033') { // if the first value is esc
        getchar(); // skip the [
        switch(getchar()) { // the real value
        case 'A':
            printf("arrow up \n");
            break;
        case 'B':
            printf("arrow down \n");
            break;
        case 'C':
            printf("arrow right \n");
            break;
        case 'D':
            printf("arrow left \n");
            break;
        }
    }
    if (getchar() != '\033'){
        printf("non arrow \n");
    }
}

However, the "non arrow" constantly appears even though I pressed arrow button.

If I changed the code from printf("non arrow \n") by have a variable char c, assign getchar() to it and later print c:

    if (getchar() != '\033'){
                printf("non arrow \n");
            }

The output will be as expected for arrow key (receive and print as expected) but nothing appeared when 'e' or 'r' or other single character key is entered.

I wonder what is the problem for my code and how can I modify it to receive behavior as I want.

I hope to receive answer soon.

Thank you, Huy Nguyen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2025-02-01 04:12:24

当您执行时,您正在阅读另一个字符,如果(getchar()!='\ 033'),而不是测试与第一次测试相同的字符。

使用else,而不是再次调用getchar()

while(true){
    if (getchar() == '\e') { // if the first value is esc
        getchar(); // skip the [
        switch(getchar()) { // the real value
        case 'A':
            printf("arrow up \n");
            break;
        case 'B':
            printf("arrow down \n");
            break;
        case 'C':
            printf("arrow right \n");
            break;
        case 'D':
            printf("arrow left \n");
            break;
        default:
            printf("non arrow\n");
        }
    } else {
        printf("non arrow \n");
    }
}

我添加了默认值: case switch ,以防它们发送不同的逃生序列。您可能还应该检查ESC[之后的第一个字符,而不仅仅是假设它是。

如果您有两个以上的条件,则可以使用switch(getchar()),就像ecc [之后的字符一样,也可以分配getchar()到变量并在条件下测试。

另外,如果您在测试相互排他性的条件时,则应使用 else 。

You're reading another character when you do if (getchar() != '\033'), not testing the same character that you tested the first time.

Use else instead of calling getchar() again.

while(true){
    if (getchar() == '\e') { // if the first value is esc
        getchar(); // skip the [
        switch(getchar()) { // the real value
        case 'A':
            printf("arrow up \n");
            break;
        case 'B':
            printf("arrow down \n");
            break;
        case 'C':
            printf("arrow right \n");
            break;
        case 'D':
            printf("arrow left \n");
            break;
        default:
            printf("non arrow\n");
        }
    } else {
        printf("non arrow \n");
    }
}

I've added a default: case to the switch in case they send a different escape sequence. You should probably also check that the first character after ESC is [, not just assume it will be.

If you have more than two conditions, you can use switch(getchar()) there, like you do with the character after ESC [, or you can assign the result of getchar() to a variable and test that in the conditions.

Also, you should use else if when you're testing mutually-exclusive conditions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文