How can I write a command line shell to capture the UP key to get historical input?

I want to write a command line shell for a background program, and in witch I can enter some commands. I want to add a function like bash (or another shell) --- "UP" to get historical input. How can I press the UP key?

If I just use std::getline(std::cin, line) , I cannot get the "UP" key or another function key, i.e. Ctrl + w to remove a word from the input line.

+4
source share
3 answers

There is a readline function that supports editing history and lines, etc.

In basic mode, it reads a line, etc. If you want to add hooks, you can make the extension command by pressing tab or similar.

This is what your typical Linux shell uses.

Documentation here: http://web.mit.edu/gnu/doc/html/rlman_2.html

Example: http://www.delorie.com/gnu/docs/readline/rlman_48.html

Project main page: http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html

+3
source

Use kbhit() to get keyboard arrow keys

+1
source

Use the ncurses library, see an example program :

 #include<ncurses.h> #include<iostream> int main() { int ch; initscr(); //initialize the ncurses data structures keypad(stdscr,TRUE); //enable special keys capturing ch=getch(); if(ch==KEY_UP) std::cout << "Key up pressed!" << std::endl; } 
0
source

Source: https://habr.com/ru/post/1499593/


All Articles