Note that there are some explanatory texts on larger screens.

plurals
  1. POC detect arrow keys with termios doesn't work well
    primarykey
    data
    text
    <p>I have the problem that when you press an arrow key You detect 3 input and the while loop is executed 3 times, in fact the print of the string PRINT OF DEBUG occurs three times. Why? I would that when you press a arrow key the loop is executed 1 time. How do I do it? Here the code:</p> <pre><code>#include &lt;termios.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;fcntl.h&gt; #define KEYCODE_R 0x43 #define KEYCODE_L 0x44 #define KEYCODE_U 0x41 #define KEYCODE_D 0x42 int main() { int c; printf("\nPRESS A ARROW\n"); while(1) { c = getch(); printf("\nPRINT OF DEBUG\n"); switch(c) { case KEYCODE_L: //rilevazione pressione freccia sinistra printf("\nLEFT\n"); break; case KEYCODE_R: printf("\nRIGHT\n"); break; case KEYCODE_U: printf("\nFORWARD\n"); break; case KEYCODE_D: printf("\nINDIETRO\n"); break; } } return 0; } int getch() { char c; struct termios before,after; tcgetattr (STDIN_FILENO, &amp;before);//STDIN_FILENO e' il File Descriptor dello stdin. Salviamo in before la configurazione attuale dello stdin memcpy(&amp;after, &amp;before, sizeof(struct termios)); after.c_lflag &amp;=~ (ICANON | ECHO); //modifichiamo la configurazione dello stadin modificando il flag c_lflag con degli or e and bit a bit after.c_cc[VMIN] = 1; after.c_cc[VTIME] = 0; tcsetattr (STDIN_FILENO, TCSANOW, &amp;after); //qui effettuiamo effettivamente le modifiche if (read (STDIN_FILENO, &amp;c, sizeof(char)) == -1) //read e' una system call, leggiamo dallo stdin { perror("\nErrore in lettura\nIl programma termina"); tcsetattr (STDIN_FILENO, TCSANOW, &amp;before); exit(-1); } tcsetattr (STDIN_FILENO, TCSANOW, &amp;before); //si ripristina la configurazione dello stdin return c; } </code></pre> <p>Thank you for the attention</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload