You are not logged in.
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
consider using ncurses instead
Offline
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
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
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