在C中输入密钥检测

时间:2014-01-24 07:47:54

标签: c key enter

我有这个程序:

CODE:

#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码中所述),但没有任何反应。有人可以告诉我为什么吗?

1 个答案:

答案 0 :(得分:2)

不管怎么说,伙计们。我发现了如何。

    #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.