0

I'm new to c but not new to python so I was wondering if there was a way to move a function into python from c without any non built in modules. I learned a little bit of c just so I could understand how some of the python things worked and this is why I don't want to download anything. Anyway here is the function I want to bring into python.

int c_kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if (ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

It came from the conio.h header but I only want the one function. Thanks for the help in advance.

Simon Champney
  • 151
  • 1
  • 13
  • 1
    Yes, CPython at least provides a robust set of tools for working with things like this. Did you try looking anything up? – juanpa.arrivillaga Jun 14 '21 at 23:55
  • @juanpa.arrivillaga, I did look up some things but they don't work for my situation. To be honest it's actually because I use an online compiler. Is there an online compiler for CPython that you can suggest? – Simon Champney Jun 15 '21 at 14:01
  • `keyboard.is_pressed()` ref : https://stackoverflow.com/questions/24072790/detect-key-press-in-python – p._phidot_ Jun 15 '21 at 19:49
  • @p._phidot_, thanks for the idea. I looked into it and it doesn't seem to work for my situation. The keyboard module needs to be downloaded, I don't have that option. – Simon Champney Jun 15 '21 at 20:14

0 Answers0