【问题标题】:Enter key detection in C在 C 中输入键检测
【发布时间】:2014-01-24 07:47:54
【问题描述】:

我有这个程序:

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
main()
{
    char menitem[3][32];
    int i = 0, key;
    strcpy(menitem[0], "This is the first option.");
    strcpy(menitem[1], "And I'm the second option.");
    strcpy(menitem[2], "Don't forget me: The third option!");
    start:
    system("cls");
    printf("%s", menitem[i]);
    ret:
    key = getch();
    if(i == 0)
    {
        switch(key)
        {
            case 80: i++; goto start;
            case '\n': puts("Enter"); getch(); goto start;
            default: goto ret;
        }
    }
    else if(i == 2)
    {
        switch(key)
        {
            case 72: i--; goto start;
            case '\n': puts("Enter"); getch(); goto start;
            default: goto ret;
        }
    }
    else
    {
        switch(key)
        {
            case 80: i++; goto start;
            case 72: i--; goto start;
            case '\n': puts("Enter"); getch(); goto start;
            default: goto ret;
        }
    }
}

为什么检测不到 Enter 键?我做错了什么吗?

我尝试了所有我能找到的。

我尝试使用值 10(如 ASCII 代码中所述),但没有任何反应。谁能告诉我为什么?

【问题讨论】:

    标签: c key enter


    【解决方案1】:

    没关系,伙计们。我发现了。

        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
        #include <conio.h>
    main()
    {
        char menitem[3][32];
        int i = 0, key;
        strcpy(menitem[0], "This is the first option.");
            strcpy(menitem[1], "And I'm the second option.");
            strcpy(menitem[2], "Don't forget me: The third option!");
            start:
            system("cls");
            printf("%s", menitem[i]);
            ret:
            key = getch();
            if(i == 0)
            {
            switch(key)
            {
                case 80: i++; goto start;
                case 13: puts("Enter"); getch(); goto start;
                default: goto ret;
            }
    }
    else if(i == 2)
    switch(key)
    {
        case 72: i--; goto start;
        case 13: puts("Enter"); getch(); goto start;
        default: goto ret;
    }
    else
    {
        switch(key)
        {
            case 80: i++; goto start;
            case 72: i--; goto start;
            case 13: puts("Enter"); getch(); goto start;
            default: goto ret;
        }
    }
    }
    

    值为 13。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2013-07-22
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      相关资源
      最近更新 更多