NoNameY0
NoNameY0

Reputation: 632

Simulating shell like behavior

So what I want is when the user presses the up button, I want to instantly display the command and not show the ^[[A I already know how to identify up and down from the keyboard, and I know termios might be the solution, but coming from a java background, I don't quite know how to do this. Ncurses is not an option, due to certain constraints. If you can, please help.

What I expect to happen is, when the user presses the up button, I want to instantly do a printf on the same line

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

    char input[100];
    char *history[5];
    int currentHistory=0;
    int historyCount=5;
    history[0]="hi";
    history[1]="bye";
    history[2]="computer";
    history[3]="science";
    history[4]="Yo";


    while(input[0]!='Q')
    {
       if(input[0]==LEFT_ARROW;
    }


}

Upvotes: 1

Views: 129

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

It isn't clear if you want to handle your input better, or your terminal handling better.

If you want to handle input better, I suggest wrapping your character recogonition into a function that returns an integer, which has defines for each "handled" key. That way your input looks more like

int char = readChar(input);
if (char == KEY_UP_ARROW) {
  ...
}

where readChar detects if the first key is an escape and then "peeks" to see if there is extra information available (to differentiate between a stand alone escape and the arrow keys).

If you intend to handle the screen side of things better, and your intent is to drive the terminal with your own wrapped terminal handling software, you need to reference the VT100 terminal reference (assuming that your xterm emulates VT100 (which many do)). The VT100 codes are described here.

Note that the operating system does some extra processing on input (to combine key combinations, making programs easier to differentiate between deadkey keyboard setups, etc.), so you might to disable this in-terminal processing by putting the terminal in a different "mode". Typical modes include "cooked", and "raw", where "raw" provides you with key codes that are not combined for simplicity.

Upvotes: 1

sfstewman
sfstewman

Reputation: 5677

You'll find this functionality, and more, in either the GNU readline or the BSD editline libraries. They offer largely the same functionality, however the GNU readline library is under a GPL license, and editline is under a BSD license.

Upvotes: 4

Related Questions