How to make arrow keys and backspace work correctly when asking input from user in C program using termios.h?

前端 未结 2 605
无人共我
无人共我 2021-02-10 07:52

So I have the following code which basically just reads characters user inputs and prints them until \'q\' is entered.

#include
#include

        
2条回答
  •  时光取名叫无心
    2021-02-10 08:29

    Actually, for handling the arrow keys, you would have to implement a good-sized chunk of ncurses. There are pros/cons: the main drawback to using ncurses in a command-line application might be that it usually clears the screen. However, (n)curses provides a function filter. There is a sample program "test/filter.c" in the ncurses source which illustrates this by using the left-arrow key as an erase character, and passes the resulting line to system() to run simple commands. The sample is less than 100 lines of code -- simpler and more complete than the examples above, it seems.

提交回复
热议问题