How to properly print special key sequences with terminfo in terminal-agnostic way?

StackOverflow https://stackoverflow.com/questions/15728376

  •  30-03-2022
  •  | 
  •  

Pregunta

I am trying to emulate a user, pressing such special keys as <Left Arrow>, <Backspace>, <Delete> and so on. I heard that curses/terminfo might help to do that in terminal-agnostic way, but when i try (with following code) to print first string "text" and then emmit a key_left sequence, i am not getting a (with '|' as cursor) "tex|t", but rather "textD|". Why? How to do that properly?

#include <term.h>
#include <stdio.h>

static void putf(const char *name) {
  putp(name);
  fflush(stdout);
}

int main(int argc, char **argv) {
  setupterm((char*)0, 1, (int*)0);

  printf("text");
  fflush(stdout);

  putf(key_left);

  // hang up until user input
  fgetc(stdin);

  return 0;
}

Please note, while i am okay with using curses, ncurses is unacceptable in my use-case. Also, note that i don't want to use something like initscr(), replacing current terminal screen with blank one, it is not a desired behavior.

¿Fue útil?

Solución

Escape sequences responsible for cursor movement, and escape sequences associated with arrow keys, are generally distinct and should not be used instead of each other. In the terminfo database, the former are prefixed with cursor and the latter with key.

Thus, to move the cursor to the left, one should use putp (cursor_left), not putp (key_left).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top