情况KEY_ENTER无法正常工作 [英] case KEY_ENTER not functioning as expected

查看:294
本文介绍了情况KEY_ENTER无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个响应键盘输入的程序.目前,以下菜单正常运行,但是按Enter键时,我遇到了问题.当我按Enter键时,什么也没发生.我只是想知道为什么会这样? 非常感谢!

I am trying to create a program that response to keyboard input. At the moment the menu below functions correctly but upon pressing enter I am running into issues. When I press enter nothing happens. I am just wondering why this is happening? Many thanks!

#include <ncurses.h>

#define MENUMAX 6

void drawmenu(int item)
{
        int c;
        char mainmenu[] = "Menu";
        char menu[MENUMAX] [10] = {
                "1",
                "2",
                "3",
                "4",
                "5",
                "6"
        };

        clear();
        attron(A_BOLD | A_UNDERLINE);
        addstr(mainmenu);
        attroff(A_BOLD | A_UNDERLINE);
        for( c = 0; c < MENUMAX; c++)
        {
                if( c == item)
                        attron(A_REVERSE);
                attron(A_BOLD);
                mvaddstr(3 + (c * 2), 20, menu[c]);
                attroff(A_BOLD);
                attroff(A_REVERSE);
        }
        refresh();
}

int main(int argc, char *argv[])
{
        int key, menuitem;
        menuitem = 0;
        initscr();
        drawmenu(menuitem);
        keypad(stdscr, TRUE);
        noecho();
        do
        {
                raw();
                nonl();
                key = getch();
                switch(key)
                {
                        case KEY_DOWN:
                                menuitem++;
                                if(menuitem > MENUMAX - 1) menuitem = 0;
                                break;
                        case KEY_UP:
                                menuitem--;
                                if(menuitem < 0) menuitem = MENUMAX - 1;
                                break;
                        case KEY_ENTER:
                                mvaddstr(17, 25, "Test mesage!");
                                refresh();
                                break;
                        default:
                                break;
                }
                drawmenu(menuitem);
        } while(key != '~');

        echo(); 


        endwin();
        return 0;
}

推荐答案

The program has not provided for character-at-a-time (unbuffered) input, as described in the Initialization section of the ncurses manual page. Since it looks for special keys such as KEY_UP, that means it should use cbreak (rather than raw, which prevents ncurses from decoding special keys).

这篇关于情况KEY_ENTER无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆