views:

474

answers:

2

I'm reading from the standard input using the read() system call but there's a tiny thing that bothers me. I can't use the arrow keys... What I really wanted to do was to use arrow keys to go back and forth within the typed text but I think that's not that easy... So, what I at least want to do, is to ignore them.

Right now, pressing any of the arrow keys produces strange output and I want to prevent anything from being written to the standard output (consequently read from the standard input in my read() system call).

Is this easily possible to achieve or it's not that easy?

+2  A: 

In order to interpret the arrow keys the way you would ideally like to (i.e. to move back and forth and edit the input), you generally need to use a library. For Linux, the standard is GNU Readline. Hopefully someone else can say what you would normally use for a Windows CLI app.

Tyler McHenry
That's probably the only answer I'll need lol...
Nazgulled
A: 

The answer ultimately depends on where the keys come from. I ran this program under Cygwin:

int main(void)
{
    int c=0;

    while( c != 'X' ) {
        c = getchar();
        printf("\nc=%d", c);
    }
}

Every time a cursor key comes along, I see escape (27), a bracket, plus another character. So, if you get results like that, you can skip 3 keys every time you see a 27. You could also look at them and make use of them!

As mentioned, YMMV, especially for the O.S., and the actual key-getting function you call.

gbarry