Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Come posso leggere un tasto in input in tempo reale?
Forum - C/C++ - Come posso leggere un tasto in input in tempo reale?

Avatar
lorenzoscarrone (Normal User)
Pro


Messaggi: 92
Iscritto: 16/11/2011

Segnala al moderatore
Postato alle 17:46
Martedì, 26/05/2015
Salve a tutti,

come faccio a leggere un tasto in tempo reale in c, per intenderci esiste una funzione tipo readkey del pascal che mi permette di catturare un entrata da tastiera?

Volevo fare un menu interattivo da terminale con delle opzioni tali che se premo 'w' il cursore sale se premo 's' il cursore scende

io ho fatto questo ma non funziona per nulla

Codice sorgente - presumibilmente C++

  1. void MenuOptions(int pointer){
  2.     system("clear");
  3.     char p[5][4] = {"   ","   ","   ","   ","   "};
  4.     strcpy(&p[pointer][4], "-->");
  5.     printf("\t\tMenu Options:\n");
  6.     printf("%s\t\t1) New Game\n",p[0]);
  7.     printf("%s\t\t2) Load Game\n",p[1]);
  8.     printf("%s\t\t3) Controls\n",p[2]);
  9.     printf("%s\t\t4) Change monster\n",p[3]);
  10.     printf("%s\t\t5) Exit\n",p[4]);
  11. }
  12.  
  13. void Menu(){
  14.     int arrow = 0;
  15.     char key;
  16.     contr.up = 'w';
  17.     contr.down = 's';
  18.     MenuOptions(arrow);
  19.     while (( key = getchar() )!='\n') {
  20.         system("clear");
  21.         if (key == contr.up  )arrow++;
  22.         if (key == contr.down)arrow--;
  23.         if (arrow<0) arrow=4;
  24.         if (arrow>4) arrow=0;
  25.         MenuOptions(arrow);
  26.     }
  27.     switch (arrow) {
  28.         case 0:
  29.             NewGame();
  30.             break;
  31.         case 1:
  32.             ContinueGame();
  33.             break;
  34.         case 2:
  35.             ChangeControls();
  36.             break;
  37.         case 3:
  38.             ChoosePlayer();
  39.             break;
  40.         case 4:
  41.           //  exit(0);
  42.             break;
  43.         default:
  44.             break;
  45.     }
  46. }


PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 20:03
Martedì, 26/05/2015
Dipende dal tuo sistema operativo. Cosa stai usando?


Il mio blog: https://piero.dev
PM Quote
Avatar
lorenzoscarrone (Normal User)
Pro


Messaggi: 92
Iscritto: 16/11/2011

Segnala al moderatore
Postato alle 9:12
Mercoledì, 27/05/2015
Mac e Linux, anche se dovrebbero essere entrambi sistemi unix

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 15:45
Mercoledì, 27/05/2015
Codice sorgente - presumibilmente C++

  1. #include <unistd.h>
  2. #include <termios.h>
  3.  
  4. char getch() {
  5.         char buf = 0;
  6.         struct termios old = {0};
  7.         if (tcgetattr(0, &old) < 0)
  8.                 perror("tcsetattr()");
  9.         old.c_lflag &= ~ICANON;
  10.         old.c_lflag &= ~ECHO;
  11.         old.c_cc[VMIN] = 1;
  12.         old.c_cc[VTIME] = 0;
  13.         if (tcsetattr(0, TCSANOW, &old) < 0)
  14.                 perror("tcsetattr ICANON");
  15.         if (read(0, &buf, 1) < 0)
  16.                 perror ("read()");
  17.         old.c_lflag |= ICANON;
  18.         old.c_lflag |= ECHO;
  19.         if (tcsetattr(0, TCSADRAIN, &old) < 0)
  20.                 perror ("tcsetattr ~ICANON");
  21.         return (buf);
  22. }



Oppure usa getch dalla libreria curses.


Il mio blog: https://piero.dev
PM Quote