1

基本上,我的控制台应用程序有按键检测,由于某种原因它没有检测到功能键。

这是我的代码,我在 linux 上使用 GNU 编译器。任何帮助或想法将不胜感激。

        refresh();
        key = getch();
        switch(key) {
            case KEY_HOME:      key = HOME;   break;
            case KEY_END:       key = END;    break;
            case KEY_UP:        key = UP;     break;
            case KEY_DOWN:      key = DOWN;   break;
            case KEY_LEFT:      key = LEFT;   break;
            case KEY_RIGHT:     key = RIGHT;  break;
            case KEY_NPAGE:     key = PGDN;   break;
            case KEY_PPAGE:     key = PGUP;   break;
            case KEY_DC:        key = DEL;    break;
            case KEY_IC:        key = INSERT; break;
            case KEY_F(1):      key = F(1);   break;
            case KEY_F(2):      key = F(2);   break;
            case KEY_F(3):      key = F(3);   break;
            case KEY_F(4):      key = F(4);   break;
            case KEY_F(5):      key = F(5);   break;
            case KEY_F(6):      key = F(6);   break;
            case KEY_F(7):      key = F(7);   break;
            case KEY_F(8):      key = F(8);   break;
            case KEY_F(9):      key = F(9);   break;
            case KEY_F(10):     key = F(10);  break;
            case KEY_F(11):     key = F(11);  break;
            case KEY_F(12):     key = F(12);  break;
            case KEY_ENTER:     key = ENTER;  break;
            case KEY_BACKSPACE: key = BACKSPACE; break;
            default:
                //key = F(2); //For any function keypress, it jumps to default
                if (NON_ASCII(key) != 0)
                    key = UNKNOWN;
        }    
4

3 回答 3

2

我不是诅咒专家,但阅读一些手册页让我了解了这个程序:

#include <curses.h>

int main()
{
    int key;

    initscr(); cbreak(); noecho();

    while (1)
    {

        key = getch();
        printw ("%u\n", key);
    }

    return 0;
}

当我按下 F 键时,我得到一个 3 个字符的序列:27, 79, (80 + N-1) 其中 N 是 F 键的编号。我认为你的开关必须认识到密钥是一个转义序列并特别处理它。

编辑:该模式仅适用于 F1-F4。F5 改变它。您可能希望从 curses 中合并 F(n) 宏。

于 2010-08-12T15:22:37.547 回答
0

有同样的问题,个人。

将 F(n) 宏转换为 char 类型神奇地为我解决了这个问题;

cmd = getch();
    switch(cmd){
        case 'r':
            addch('r');
            break;
        case 'w':
            addch('x');
            break;
        default:
            if(cmd == (char)KEY_F(2)){ endwin(); exit(0); }
    }

and the like. Worked for F2 through F10 plus F12. F's 1, 10 and 11 are "occupied" for lack of a better word on my xterm. (F1 opens the help window, F11 toggles fullscreen mode, etc.) Again, I can't emphasize enough that I couldn't begin to guess why that works.

于 2012-08-09T07:49:11.713 回答
0

You may need to enable the "keypad" functionality of the terminal using the keypad function. From the keypad(3x) manual page:

int keypad(WINDOW *win, bool bf);

The keypad option enables the keypad of the user's terminal. If enabled (bf is TRUE), the user can press a function key (such as an arrow key) and wgetch returns a single value representing the function key, as in KEY_LEFT. If disabled (bf is FALSE), curses does not treat function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made to transmit) and off (made to work locally), turning on this option causes the terminal keypad to be turned on when wgetch is called. The default value for keypad is false.

于 2013-12-24T01:35:29.137 回答