You are not logged in.

#1 2007-02-27 18:05:31

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

C, Managing Input, detecting pressing of PGUP, ARROWS, ECT...

Here a scrap of code I wrote:

/*GNU 99 C */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>

void writeself(int chr, FILE* stream)
{
    fprintf(stream, "%c ", chr);
}

int input(FILE* stream) {
    struct termios oldt, newt;
    int buf[PBSH_MAXCCL];
    int chr;
    const int stream_fileno = fileno(stream);

    tcgetattr(stream_fileno, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO); 
    tcsetattr(stream_fileno, TCSANOW, &newt );
    for (;;) {
        chr = fgetc(stream);
        writeself(chr, stdout);
    }
    tcsetattr(stream_fileno, TCSANOW, &oldt );
}

int main()
{
    input(stdin);
}

My problem is related to `strange' keys, like PGUP, PGDOWN.
They draw sequence of characters usually beginning of 0x1b but they seems to change in length (Right arrow is :0x1b[A, F5 is 0x1b[15~) or even in contests (F5 is 0x1b[15~ while `talking' with login and is 0x1b[[E while `talking' with getty).
I'd like being able to detect pressing of any key for programming particular effects like history or Vim modes.

Any help? I am becoming crazy!

Last edited by ezzetabi (2007-02-27 18:05:50)

Offline

#2 2007-02-28 16:25:44

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 125

Re: C, Managing Input, detecting pressing of PGUP, ARROWS, ECT...

consider using ncurses instead

Offline

#3 2007-02-28 18:30:15

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: C, Managing Input, detecting pressing of PGUP, ARROWS, ECT...

bboozzoo has the right of it.  The only portable way to do something like that is to use a portable library that handles all the differences.  ncurses does this, as does SLang, and a few others (BSD has a special library too, IIRC).

I'd recommend using ncurses myself.

Offline

#4 2007-02-28 20:28:49

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Re: C, Managing Input, detecting pressing of PGUP, ARROWS, ECT...

All right, I'll use ncurses. But it sounds strange, after all nor bash, nor sh and maybe not even vim depends on ncurses...
I am still missing something.

Thank a lot.

Offline

#5 2007-02-28 21:16:56

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: C, Managing Input, detecting pressing of PGUP, ARROWS, ECT...

vim uses it's own drawing toolkit that does mostly what ncurses does.  Just because something doesn't depend on ncurses, doesn't mean it doesn't do the same thing.
Bash itself uses libreadline to get at information such as "meta [A" (which is \e[A or ^[[A) and says "ah meta [A is 'up').  It doesn't expose this information.

OTOH, ncurses correctly identifies key presses based on terminal type.  For instance, Home/End keys can be different on different terminals (see /etc/inputrc for some of the values).  ncurses knows this as well and is able to correctly determine that the keypress is a KEY_HOME press.

Your options are: use ncurses and make it easy, or write something yourself to parse and understand key presses on different terminals.

Offline

Board footer

Powered by FluxBB