Advertisement
AntonioVillanueva

Lectura de tecla Linux no bloquea

Mar 3rd, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <termios.h>
  4. #include <sys/time.h>
  5.  
  6.  using namespace std;
  7. int unix_text_kbhit(void)
  8. {
  9.     struct timeval tv = { 0, 0 };
  10.     fd_set readfds;
  11.  
  12.     FD_ZERO(&readfds);
  13.     FD_SET(STDIN_FILENO, &readfds);
  14.  
  15.     return select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) == 1;
  16. }
  17.  
  18. void mode_raw(int activer) //Terminal en modo RAW
  19. {
  20.     static struct termios cooked;
  21.     static int raw_actif = 0;
  22.  
  23.     if (raw_actif == activer)
  24.         return;
  25.  
  26.     if (activer)
  27.     {
  28.         struct termios raw;
  29.  
  30.         tcgetattr(STDIN_FILENO, &cooked);
  31.  
  32.         raw = cooked;
  33.         cfmakeraw(&raw);
  34.         tcsetattr(STDIN_FILENO, TCSANOW, &raw);
  35.     }
  36.     else
  37.         tcsetattr(STDIN_FILENO, TCSANOW, &cooked);
  38.  
  39.     raw_actif = activer;
  40. }
  41. int main (){
  42.     while (true){      
  43.         mode_raw(1);
  44.         if (unix_text_kbhit()!=0){mode_raw(0);cout<<(char) getchar()<<endl;}
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement