Keyboard input: how to separate keycodes received from user
- by Iulian Serbanoiu
Hello,
I am writing an application involving user input from the keyboard. For doing it I use this way of reading the input:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int mygetch( ) {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int main(void)
{
int c;
do{
c = mygetch();
printf("%d\n",c);
}while(c!='q');
return 0;
}
Everyting works fine for letters digits,tabs but when hiting DEL, LEFT, CTRL+LEFT, F8 (and others) I receive not one but 3,4,5 or even 6 characters.
The question is: Is is possible to make a separation of these characters (to actually know that I only hit one key or key combination).
What I would like is to have a function to return a single integer value for any type of input (letter, digit, F1-F12, DEl, PGUP, PGDOWN, CTRL+A, CTRL+ALT+A, ALT+LEFT, etc). Is this possible?
I'm interested in an idea to to this, the language doesn't matter much, though I'd prefer perl or c.
Thanks,
Iulian