<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<div><span class="Apple-style-span" style="font-size: 12pt; ">Bueno no soy un lumbreras pero ésta demo o idea rápida de cómo hacerlo te podría ayudar, me voy al catre lo siento, recuerda compilar la aplicación con la </span><b><span class="Apple-style-span" style="font-size: 12pt; ">librería de hilos POZI</span></b><span class="Apple-style-span" style="font-size: 12pt; ">...</span></div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">gcc -lpthread ...</font></div><div><br></div><div><div><font class="Apple-style-span" face="'Courier New'">#include <stdio.h></font></div><div><font class="Apple-style-span" face="'Courier New'">#include <stdlib.h></font></div><div><font class="Apple-style-span" face="'Courier New'">#include <unistd.h></font></div><div><font class="Apple-style-span" face="'Courier New'">#include <termio.h></font></div><div><font class="Apple-style-span" face="'Courier New'">#include <sys/ioctl.h></font></div><div><font class="Apple-style-span" face="'Courier New'">#include <pthread.h></font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">#define STDINFD  0</font></div><div><font class="Apple-style-span" face="'Courier New'">#undef getc</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">char teclaPulsada(void);</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">void *funcionAplicacion(void *);</font></div><div><font class="Apple-style-span" face="'Courier New'">void *funcionDetectaTecla( void *);</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">// variable global que reocge las teclas</font></div><div><font class="Apple-style-span" face="'Courier New'">char tecla=0;</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">int main(void)</font></div><div><font class="Apple-style-span" face="'Courier New'">{</font></div><div><font class="Apple-style-span" face="'Courier New'">    pthread_t detectaTecla, aplicacion;</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'Courier New'">      </font></span></div><div><font class="Apple-style-span" face="'Courier New'">    pthread_create( &aplicacion  , NULL, funcionAplicacion, NULL);</font></div><div><font class="Apple-style-span" face="'Courier New'">    pthread_create( &detectaTecla, NULL, funcionDetectaTecla, NULL);</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    pthread_join( aplicacion, NULL);  </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    pthread_join( detectaTecla, NULL); </font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    // no llega hasta aquí, porque detectaTEcla está en un bucle infinito</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    return 0;</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">void *funcionDetectaTecla( void * )</font></div><div><font class="Apple-style-span" face="'Courier New'">{</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    while(true)</font><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'Courier New'">   </font></span><font class="Apple-style-span" face="'Courier New'">tecla=teclaPulsada();</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">void *funcionAplicacion( void * )</font></div><div><font class="Apple-style-span" face="'Courier New'">{</font></div><div><font class="Apple-style-span" face="'Courier New'">    struct termio parametro_anterior;</font></div><div><font class="Apple-style-span" face="'Courier New'"> </font></div><div><font class="Apple-style-span" face="'Courier New'">    ioctl(STDINFD,TCGETA,&parametro_anterior); // hacemos un backup, TC get</font></div><div><span class="Apple-style-span" style="font-family: 'Courier New'; "><br></span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; ">    // Aquí estaría la aplicación en si...</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    while(tecla!=27) {</span></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'Courier New'">            </font></span><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    </font><span class="Apple-style-span" style="font-family: 'Courier New'; ">    </span><span class="Apple-style-span" style="font-family: 'Courier New'; ">printf("...y sigue... (tecla pulsada= %d )\n",tecla);</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    }</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    // Habría que matar el otro hilo o buscar una forma mejor...</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    // de lo contrario hay que dejar todo como estaba si no...</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    // no podremos ver las teclas al pulsarlas y tendremos una ...</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    // terminal fantasma...</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    ioctl(STDINFD,TCSETA,&parametro_anterior); // lo dejamos como estaba TC set</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'Courier New'">     </font></span><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'"></font><span class="Apple-style-span" style="font-family: 'Courier New'; ">    exit(0); // salimos a la fuerza</span></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">char teclaPulsada(void) {</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    char caracter;</font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'">    struct termio parametro_anterior, parametro;</font></div><div><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'"></font><span class="Apple-style-span" style="font-family: 'Courier New'; ">    ioctl(STDINFD,TCGETA,&parametro_anterior); // hacemos un backup, TC get</span></div><div><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'"></font><span class="Apple-style-span" style="font-family: 'Courier New'; ">    parametro = parametro_anterior;</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    parametro.c_lflag &= ~(ICANON|ECHO);</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    parametro.c_cc[4] = 1;</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    </span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; ">    ioctl(STDINFD,TCSETA,&parametro); // le pasamos los nuevos paramentros</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; ">    fflush(stdin); fflush(stderr); fflush(stdout); // vaciamos los bufferes</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    read(STDINFD,&caracter,1); // Leemos un byte del buffer y lo asignamos al caracter</span></div><div><font class="Apple-style-span" face="'Courier New'"> </font></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'Courier New'"></font><span class="Apple-style-span" style="font-family: 'Courier New'; ">    ioctl(STDINFD,TCSETA,&parametro_anterior); // lo dejamos como estaba TC set</span></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span class="Apple-style-span" style="font-family: 'Courier New'; "></span><span class="Apple-style-span" style="font-family: 'Courier New'; ">    return caracter;</span></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><br></div></div>                                        </body>
</html>