I have written a text editor.
I am looking for a reliable way to differentiate between a single
Esc Keystroke and an Escape sequence generated by a special key (arrow
function key etc...).
This problem seams trivial, and I solved a years ago on a SCO UNIX
V.3.2. Here is a sample of code. This code is in a process that do only
this. It reads keystroke and pipe it to another process and back in input.
// Do some modification to the UNIX Terminal setting
struct termio tchange;
ioctl(0,TCGETA,&tchange);
tchange.c_lflag=0;
tchange.c_iflag=IGNBRK|IGNPAR;
tchange.c_cc[VMIN]=1;
tchange.c_cc[VTIME]=1;
ioctl(0,TCSETA,&tchange);
baud_delai = twice the reception time of a single character
(based on baud rate)
while (1){
char array[10];
int nb = read (0,array,10);
// This return as soon as one byte is available, waiting a little
// for the remaining 9.
if (array[0] == 27){
// Checks for more, with a timeout
fd_set inset;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = baud_delai;
memset (&inset,0,sizeof(inset));
FD_SET (handle,&inset);
if (select (1,&inset,NULL,NULL,uvtimeout == 0 ? NULL : &timeout) > 0){
nb += read (0,array+1,9);
}
}
// nb holds the number of byte in array
// Send the stuff to the other process.
}
Then I have ported this to SUNOS.
This code works well with on SUNOS, with some exception. I get equivalent
results using poll(). If you are operating in a xterm, this works fine. From
there you do a rlogin to another workstation, and exec the program. Then
the program see escape sequence as an escape keytroke and a sequence. I have
tried to increase the delai. It helps a little bit, but is unreliable.
There must be a better way to do this, because vi has no problem.
Any clue!
--
--------------------------------------------------------
Today it's my opinion