Re: Linux c++ rozhodování stiskem klávesy v konzoli

Miroslav Mraz mrazik na volny.cz
Pátek Září 3 16:12:46 CEST 2021


10 minut :

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

class Keyboard {
   struct termios initial;
   public:
     explicit Keyboard () {
       tcgetattr(0, &initial);
       struct termios my_settings;
       memcpy (&my_settings, &initial, sizeof (struct termios));
       my_settings.c_lflag    &= ~ICANON;
       my_settings.c_lflag    &= ~ECHO;
       my_settings.c_cc[VMIN]  = 1;
       my_settings.c_cc[VTIME] = 0;
       tcsetattr(0, TCSANOW, &my_settings);
     }
     ~Keyboard () {
       tcsetattr(0, TCSANOW, &initial);
     }
     char getc () const {
       char c;
       ssize_t r = read (0, &c, 1);
       if (r) return c;
       else   return '\0';
     }
};

int main () {
   Keyboard kbd;
   for (;;) {
     const char c = kbd.getc();
     printf ("key \'%c\', code 0x%02X pressed\n", c, (unsigned char) c);
     if (c == 'q') break;
   }
   return 0;
}
Mrazík

Dne 03. 09. 21 v 16:01 Pavel Hudeček napsal(a):
> Aha, no já se na to nakonec vykašlal tím způsobem, že za něco jako
> "Start the beam souce and press M to measure, or press R to repeat 
> previous, S to skip this, A to abort:"
> Dopíšu ještě
> "and press Enter"
> 


Další informace o konferenci Hw-list