3

I've read from the GNU getline documentation that it's capable for binding some callback functions to some keys. I know already how to bind an action to the TAB key using rl_bind_key function.

But how can I use it to bind some action to the following keys?: CTRL + TAB, ESC, PAUSE/BREAK

Luke Bennett
  • 32,786
  • 3
  • 30
  • 57
SasQ
  • 14,009
  • 7
  • 43
  • 43

1 Answers1

5
#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> ");
     }
}

If your are running a GNU system (or one of its variants) then run:

info readline "command line editing" "introduction" # notation convention
info readline "programming" "readline" "biding" # biding functions
xojoc
  • 171
  • 1
  • 4
  • Unfortunately it doesn't work as I expect. No matter whether I press TAB or Ctrl+TAB (or any other combination with TAB), it shows the output from the hook function "key pressed: 9". There's also something strange about the Esc key: I need to press it twice, because the first press doesn't make anything visible. Pause/Break doesn't display anything. And function keys F1..F12 give the same key code as Esc (255), but additionaly inserts some gibberish in the command line. That's why I asked this question. – SasQ Aug 02 '12 at 05:27
  • I'm not having the same problems, did you find a solution SasQ? – goji Aug 11 '13 at 02:22