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

我只是一个虾纸丫 提交于 2019-12-13 00:09:19

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/15728376/how-to-properly-print-special-key-sequences-with-terminfo-in-terminal-agnostic-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!