【问题标题】:GNU readline and key bindingsGNU readline 和键绑定
【发布时间】:2023-04-06 00:31:01
【问题描述】:

我从 GNU getline 文档中了解到它能够将某些回调函数绑定到某些键。我已经知道如何使用rl_bind_key 函数将操作绑定到 TAB 键。

但是我如何使用它来将一些操作绑定到以下键?: CTRL + TAB, ESC, PAUSE/BREAK

【问题讨论】:

    标签: gnu readline key-bindings


    【解决方案1】:
    #include <stdio.h>
    
    #include <readline/readline.h>
    
    int my_cool_readline_func (int count, int key) {
       printf ("key pressed: %d\n", key);
       rl_on_new_line ();
       return 0;
    }
    
    int main(void) {
         rl_command_func_t my_cool_readline_func;
         rl_bind_key ('\t', my_cool_readline_func);
         rl_bind_key (27, my_cool_readline_func); /* ascii code for ESC */
         rl_bind_keyseq ("\\C-a", my_cool_readline_func);
    
         while (1) {
             char *line = readline ("rl> ");
         }
    }
    

    如果您正在运行 GNU 系统(或其变体之一),则运行:

    info readline "command line editing" "introduction" # notation convention
    info readline "programming" "readline" "biding" # biding functions
    

    【讨论】:

    • 不幸的是,它没有按我的预期工作。无论我按下 TAB 还是 Ctrl+TAB(或与 TAB 的任何其他组合),它都会显示挂钩函数“key”的输出按下:9"。 Esc 键还有一些奇怪的地方:我需要按两次,因为第一次按下不会使任何东西可见。 Pause/Break 不显示任何内容。并且功能键 F1..F12 提供与 Esc (255) 相同的键代码,但另外在命令行中插入了一些乱码。这就是我问这个问题的原因。
    • 我没有遇到同样的问题,你找到 SasQ 的解决方案了吗?
    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2010-11-06
    • 2015-03-10
    相关资源
    最近更新 更多