Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5368

Python • Re: Entering digits without pressing "Enter'

$
0
0
This is C code that changes the input mode to single key response without waiting for enter. (It shouldn't be this difficult.)

Code:

#include <termios.h>int readkey()  {  int n;  unsigned char c;    n = read(STDIN_FILENO,&c,1);  if(n == 0)    return(0);  // no key press  return(c);  }    /************ SET INPUT MODE *************setkeymode(1) - readkey() returns key without waiting for enter                     OR returns immediately if no key press setkeymode(0) - restore original key mode (only after setkeymode(1))                      that waits for enter***************************************/  int setkeymode(int setflag)  {  int oldflag;  struct termios tattr;  static int flag = 0;  static int savflag = 0;  static struct termios saved_attributes;  if(savflag == 0)      {  // Save the terminal attributes so we can restore them later    tcgetattr(STDIN_FILENO, &saved_attributes);    savflag = 1;    }      oldflag = flag;    if(flag == setflag)    return(oldflag);       if(setflag == 0)    {    tcsetattr(STDIN_FILENO,TCSANOW,&saved_attributes);     flag = 0;    return(oldflag);    }       /* Set the terminal modes. */  tcgetattr(STDIN_FILENO,&tattr);  tattr.c_lflag &= ~(ICANON|ECHO|ISIG|ECHONL|IEXTEN);                     tattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);  tattr.c_oflag = OPOST|ONLCR;     tattr.c_cflag &= ~(CSIZE | PARENB);  tattr.c_cflag |= CS8;  tattr.c_cc[VMIN] = 0;  tattr.c_cc[VTIME] = 0;  tcsetattr(STDIN_FILENO,TCSAFLUSH,&tattr);  flag = 1;   return(oldflag);  } 

Statistics: Posted by petzval — Thu Jul 25, 2024 10:49 am



Viewing all articles
Browse latest Browse all 5368

Trending Articles