GNU 编译器 KEY_F 未检测到功能键

发布于 2024-09-14 05:42:39 字数 1649 浏览 13 评论 0原文

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

这是我的代码,我在 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;
        }    

Basically, I have key detection for my console application, for some reason it's not detecting function keys.

Here is my code, I'm using GNU compiler on linux. Any help or ideas would be greatly appreciated.

        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;
        }    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-09-21 05:42:39

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

#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) 宏。

I'm not a curses expert, but a bit of reading man pages netted me this program:

#include <curses.h>

int main()
{
    int key;

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

    while (1)
    {

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

    return 0;
}

When I press an F key, I get a 3-character sequence: 27, 79, (80 + N-1) where N is the number of the F key. I think your switch will have to recognize that the key is an escape sequence and handle it specially.

Edit: That pattern holds only for F1-F4. F5 changes it up. You'll probably want to incorporate the F(n) macros from curses.

所谓喜欢 2024-09-21 05:42:39

个人也有同样的问题。

将 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); }
    }

等等。曾为 F2 至 F10 加 F12 工作。 F 的 1、10 和 11 被“占用”,因为我的 xterm 上没有更好的词。 (F1 打开帮助窗口,F11 切换全屏模式等)再次强调,我无法开始猜测为什么会这样。

Having the same issue, personally.

Casting the F(n) macro to a char type magically cleared the issue for me;

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.

隐诗 2024-09-21 05:42:39

您可能需要使用keypad功能启用终端的“键盘”功能。从键盘(3x) 手册页:

int 键盘(WINDOW *win, bool bf);

键盘选项启用用户终端的键盘。如果启用
bfTRUE),用户可以按功能键(例如箭头键)并
wgetch 返回表示功能键的单个值,如下所示
KEY_LEFT。如果禁用(bfFALSE),curses 不会处理函数
特别是按键,程序必须自行解释转义序列。
终端中的键盘是否可以打开(发送)和关闭
(在本地工作),打开此选项会导致终端键盘
wgetch被调用时打开。键盘的默认值为 false。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文