2008-10-21 26 views

Respuesta

8

No hay una manera estándar, pero esto debería comenzar.

de Windows:

getch(); 

Unix:

utilizar el código del libro de programación Unix W. Richard Stevens' para configurar su terminal en modo sin procesar, y después de su utilización Lea().

static struct termios save_termios; 
static int    term_saved; 

int tty_raw(int fd) {  /* RAW! mode */ 
    struct termios buf; 

    if (tcgetattr(fd, &save_termios) < 0) /* get the original state */ 
     return -1; 

    buf = save_termios; 

    buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); 
        /* echo off, canonical mode off, extended input 
         processing off, signal chars off */ 

    buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON); 
        /* no SIGINT on BREAK, CR-toNL off, input parity 
         check off, don't strip the 8th bit on input, 
         ouput flow control off */ 

    buf.c_cflag &= ~(CSIZE | PARENB); 
        /* clear size bits, parity checking off */ 

    buf.c_cflag |= CS8; 
        /* set 8 bits/char */ 

    buf.c_oflag &= ~(OPOST); 
        /* output processing off */ 

    buf.c_cc[VMIN] = 1; /* 1 byte at a time */ 
    buf.c_cc[VTIME] = 0; /* no timer on input */ 

    if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) 
     return -1; 

    term_saved = 1; 

    return 0; 
} 


int tty_reset(int fd) { /* set it to normal! */ 
    if (term_saved) 
     if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) 
      return -1; 

    return 0; 
} 
3

Lamentablemente, la norma C no tiene instalaciones para detectar eventos de teclado. Debe confiar en las extensiones específicas de la plataforma. El manejo de la señal no te ayudará.

3

Realmente debería utilizar bibliotecas de terceros. Definitivamente no existe una forma independiente de la plataforma para hacerlo en ANSI C. El manejo de la señal no es el camino.

4

¿Qué hay de bueno viejo kbhit? Si entiendo la pregunta correctamente, esto funcionará. Here es la implementación de kbhit en Linux.

Cuestiones relacionadas