简体   繁体   English

在C中使用箭头键

[英]Using Arrow Keys in C

I've finally got a hold of the Arrow Keys in C. I've found out how to have C detect them and have actually made a program of it. 我终于掌握了C中的箭头键。我发现了如何让C检测它们并实际上编写了一个程序。

The problem is... The program is bugged. 问题是...该程序存在错误。 I don't know what I've done wrong. 我不知道我做错了什么。

CODE: 码:

#include <stdio.h>
main()
{
    char menitem[3][32], key, key2;
    int i = 0;
    strcpy(menitem[0], "Option 1 [X]");
    strcpy(menitem[1], "Option 2 [ ]");
    strcpy(menitem[2], "Option 3 [ ]");
    start:
    system("cls");
    printf("%s\n%s\n%s", menitem[0], menitem[1], menitem[2]);
    key = getch();
    key2 = 0;
    if(key = 0xE0)
    key2 = getch();
    ret:
    if(i == 0)
    {
         switch(key2)
         {
        case 80:
            strcat(menitem[0], "\b\b ]");
            i++;
            strcat(menitem[i], "\b\bX]");
            goto start;
        default: goto ret;
     }
}
else if(i == 2)
{
     switch(key2)
    {
    case 72:
        strcat(menitem[2], "\b\b ]");
        i--;
        strcat(menitem[i], "\b\bX]");
        goto start;
        default: goto ret;
    }
}
else
{
    switch(key2)
    {
        case 80:
        strcat(menitem[i], "\b\b ]");
        i++;
        strcat(menitem[i], "\b\bX]");
        goto start;
        case 72:
        strcat(menitem[i], "\b\b ]");
        i--;
        strcat(menitem[i], "\b\bX]");
        goto start;
        default: goto ret;
    }
}
}

Here's the problem: 这是问题所在:

When I go up from Option 2, Option 3 turns into "X]". 当我从选项2转到上时,选项3变为“ X]”。 Any idea why? 知道为什么吗?

Try compiling it and keep on using the arrow keys. 尝试编译它并继续使用箭头键。 See what happens. 走着瞧吧。 Will appreciate any help given! 将不胜感激!

Doing /b and erasing input, then adding input back, over and over, is not a good idea. 执行/ b并擦除输入,然后一次又一次地添加输入不是一个好主意。 For one, if the user presses lots of arrow keys, you'll get tons of backspaces/erased characters and your strings will grow very large. 首先,如果用户按下很多箭头键,则会得到大量的退格键/删除的字符,并且字符串会变得非常大。 You are having problems because you are deleting too many characters in certain situations. 您遇到问题是因为在某些情况下要删除太多字符。 Instead, just do a memory modification of the character inside the []. 相反,只需对[]中的字符进行内存修改。 I will edit this post with a working solution. 我将使用有效的解决方案来编辑​​此帖子。

Edit Here is a working solution: 编辑这里是一个可行的解决方案:

#include <stdio.h>
int main()
{
    char menitem[3][32], key, key2;
    int i = 0;
    int currentlySelectedItem = 1;
    strcpy(menitem[0], "Option 1 [X]");
    strcpy(menitem[1], "Option 2 [ ]");
    strcpy(menitem[2], "Option 3 [ ]");
    while(1)
    {
        system("cls");
        printf("%s\n%s\n%s", menitem[0], menitem[1], menitem[2]);
        key = getch();
        key2 = 0;
        if(key == -32)
        {
            key2 = getch();
        }
        else
        {
            continue;
        }
        if(key2 == 80)
        {
            currentlySelectedItem++;
        }
        else if(key2 == 72)
        {
            currentlySelectedItem--;
        }

        //make sure the selected item stays in range
        if(currentlySelectedItem < 1)
            currentlySelectedItem = 1;
        if(currentlySelectedItem > 3)
            currentlySelectedItem = 3;

        menitem[0][10] = ' ';
        menitem[1][10] = ' ';
        menitem[2][10] = ' ';
        menitem[currentlySelectedItem-1][10] = 'X';

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM