Keyboard input: how to separate keycodes received from user
Posted
by Iulian Serbanoiu
on Stack Overflow
See other posts from Stack Overflow
or by Iulian Serbanoiu
Published on 2010-05-07T18:24:19Z
Indexed on
2010/05/07
18:28 UTC
Read the original article
Hit count: 365
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
© Stack Overflow or respective owner