title: how can one recognize inputs from multiple keys simultaneously in c (linux)
body: <p>I am trying to create ping pong in C using ncurses, and I have right now a huge setback because I can't figure out how I can allow two players to move the pads simultaneously. What I've tried is creating a seperate thread which would then use select to detect any buffered key presses and then put that into an array containing my controls. However, it only reads the first key and doesn't recognize the other.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ncurses.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;errno.h&gt;
  
  #define DELAY 30000
  
  #define P1_UP 0
  #define P1_DOWN 1
  #define P2_UP 2
  #define P2_DOWN 3
  
  struct player {
      int x;
      int y;
      int score;
  };
  
  void *_thread_func(void *);
  
  int main(int argc, char **argv) {
    struct player p[2];
    int x, y, max_y, max_x;
    int *keys;
    pthread_t _thread;
  
    keys = calloc(4, sizeof(int));
  
    if(pthread_create(&amp;_thread, NULL, _thread_func, &amp;keys) != 0) {
      perror("pthread_create");
      exit(EXIT_FAILURE);
    }
  
    initscr();
    noecho();
    curs_set(FALSE);
  
    getmaxyx(stdscr, max_y, max_x);
  
    p[0].score = p[1].score = 0;
    p[0].y = p[1].y = max_y/2-3; // length of pad is 6
    p[0].x = 0; // width of pad is 1
    p[1].x = max_x-1;
    while(1) {
      getmaxyx(stdscr, max_y, max_x);
  
      clear();
  
      mvprintw(p[0].y  , p[0].x  , "|");
      mvprintw(p[0].y+1, p[0].x  , "|");
      mvprintw(p[0].y+2, p[0].x  , "|");
      mvprintw(p[0].y+3, p[0].x  , "|");
      mvprintw(p[0].y+4, p[0].x  , "|");
      mvprintw(p[0].y+5, p[0].x  , "|");
  
      mvprintw(p[1].y  , p[1].x  , "|");
      mvprintw(p[1].y+1, p[1].x  , "|");
      mvprintw(p[1].y+2, p[1].x  , "|");
      mvprintw(p[1].y+3, p[1].x  , "|");
      mvprintw(p[1].y+4, p[1].x  , "|");
      mvprintw(p[1].y+5, p[1].x  , "|");
  
      refresh();
      usleep(DELAY);
  
      if(keys[P2_UP]) {
        keys[P2_UP] = 0;
        if(--p[1].y &lt; 0) p[1].y++; 
      }
  
      if(keys[P2_DOWN]) {
        keys[P2_DOWN] = 0;
        if(++p[1].y &gt;= max_y-5) p[1].y--; 
      }
  
      if(keys[P1_UP]) {
        keys[P1_UP] = 0;
        if(--p[0].y &lt; 0) p[0].y++; 
      }
  
      if(keys[P1_DOWN]) {
        keys[P1_DOWN] = 0;
        if(++p[0].y &gt;= max_y-5) p[0].y--; 
      }
    }
  
    free(keys);
  
    endwin();
  }
  
  
  void *_thread_func(void *arg) {
    fd_set readfds;
    int    num_readable;
    int    num_bytes;
    struct timeval tv;
    int    **keys;
    char   buf[1];
    int    fd_stdin;
  
    keys = (int**) arg;
    fd_stdin = fileno(stdin);
  
    while(1) {
      FD_ZERO(&amp;readfds);
      FD_SET(fileno(stdin), &amp;readfds);
  
      tv.tv_sec = 0;
      tv.tv_usec = 0;
  
      fflush(stdout);
      num_readable = select(fd_stdin + 1, &amp;readfds, NULL, NULL, &amp;tv);
      if (num_readable == -1) {
        fprintf(stderr, "\nError in select : %s\n", strerror(errno));
        exit(1);
      }
      if (num_readable &gt; 0) {
        num_bytes = read(fd_stdin, buf, 1);
        if (num_bytes &lt; 0) {
          fprintf(stderr, "\nError on read : %s\n", strerror(errno));
          exit(1);
        }
  
        switch(buf[0]) {
          case 105: /* i -&gt; ascii 105*/
            (*keys)[P2_UP] = 1;
            break;
          case 107: /* k -&gt; ascii 107*/
            (*keys)[P2_DOWN] = 1;
            break;
          case 119: /* w -&gt; ascii 119*/
            (*keys)[P1_UP] = 1;
            break;
          case 115: /* s -&gt; ascii 115*/
            (*keys)[P1_DOWN] = 1;
            break;
        }
      }
    }
  
    return NULL;
  }
  </code></pre>
  
  <p>How can I in C recognize more than one keys down at the same time? Any example on how this can be done would be greatly appreciated :)</p>