Configure key repeat delay to detect if key is being pressed

前端 未结 2 1155
春和景丽
春和景丽 2021-01-14 11:46

I am writing a program in C that uses ncurses to check if a key is being pressed. The problem is that there is a key repeat delay.

If I for example hold the key \'a\

2条回答
  •  时光说笑
    2021-01-14 12:17

    Try this to ignore buffered key repeats:

    int key;
    if ((key = getch()) != ERR) {
      while (getch() == key);
    }
    

    In context:

    //example.c
    
    #include 
    #include 
    
    int main(int argc, char *argv[]) {
      int pos_x = 0;
      int max_x = 0, max_y = 0;
      int key = 0;
      int on = 1;
    
      initscr();
      noecho();
      cbreak();
      curs_set(FALSE);
      keypad(stdscr, TRUE);
      nodelay(stdscr, TRUE);
      getmaxyx(stdscr,max_y,max_x);
    
      while(1) {
        clear();
        mvprintw(0, 0, "Press, hold and release L-R arrow keys. Press UP/DOWN to toggle function.");
        mvprintw(1, 0, "Skip buffered repeats: %s", (on ? "ON" : "OFF"));
        mvprintw(2, pos_x, "@");
        refresh();
        usleep(50000);
        getmaxyx(stdscr,max_y,max_x);
        key = getch();
    
        // skip buffered repeats                                                                                     
        if (on) {
          if (key != ERR) {
            while (getch() == key);
          }
        }
        //                                                                                                           
    
        switch (key) {
        case KEY_LEFT:
          pos_x += (pos_x > 0 ? -1 : 0); break;
        case KEY_RIGHT:
          pos_x += (pos_x < max_x - 1 ? 1 : 0); break;
        case KEY_UP:
          on = 1; break;
        case KEY_DOWN:
          on = 0; break;
        }
      }
      endwin();
    }
    

    Compile and run with gcc example.c -lncurses -oexample && ./example

提交回复
热议问题