LEE_FLECHAS.c



/* LEE TECLAS DE FLECHAS Y NOS DA SU CODIGO ASCII 
	SOLO FUNCIONA PARA LAS TECLAS DE FLECHAS
	DEBIDO A LOS TRES read

		FLECHA ARRIBA		===>		65
		FLECHA ABAJO		===>		66
		FLECHA DERECHA		===>		67
		FLECHA IZQUIERDA	===>		68
*/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <sys/ioctl.h>

#define STDINFD  0
#undef getc

char inkey(void);

int main() {
	char c;
 	printf("Presione una tecla: ");
 	c=inkey();
 	printf("Codigo ASCII de la tecla presionada: [%d] Caracter: [%c]\n",c,c);
 	return 0;
}

char inkey(void) {
 	char c;
 	struct termio param_ant, params;

 	ioctl(STDINFD,TCGETA,&param_ant);

 	params = param_ant;
 	params.c_lflag &= ~(ICANON|ECHO);
	params.c_cc[4] = 1;

	ioctl(STDINFD,TCSETA,&params);

	fflush(stdin); fflush(stderr); fflush(stdout);
	read(STDINFD,&c,1);
	read(STDINFD,&c,1);
	read(STDINFD,&c,1);

	ioctl(STDINFD,TCSETA,&param_ant);
	return c;
}