faux-getch.c 515 B

12345678910111213141516171819202122232425262728
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <termios.h>
  5. int main(void)
  6. {
  7. while (1) {
  8. int c = 0;
  9. static struct termios oldt = {};
  10. static struct termios newt = {};
  11. // Don't wait for Enter (buffer)
  12. tcgetattr(STDIN_FILENO, &oldt);
  13. newt = oldt;
  14. newt.c_lflag &= ~(ICANON);
  15. tcsetattr( STDIN_FILENO, TCSANOW, &newt);
  16. c = getchar();
  17. printf(" %u 0x%x\n", (unsigned char)c, (unsigned char)c);
  18. // Restore terminal
  19. tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
  20. }
  21. return 0;
  22. }