Kbhit.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*****************************************************************************
  2. kbhit() and getch() for Linux/UNIX
  3. Chris Giese <geezer@execpc.com> http://my.execpc.com/~geezer
  4. Release date: ?
  5. This code is public domain (no copyright).
  6. You can do whatever you want with it.
  7. *****************************************************************************/
  8. #if defined(_WIN32)
  9. #include <conio.h> /* kbhit(), getch() */
  10. #else
  11. #include <sys/time.h> /* struct timeval, select() */
  12. /* ICANON, ECHO, TCSANOW, struct termios */
  13. #include <termios.h> /* tcgetattr(), tcsetattr() */
  14. #include <stdlib.h> /* atexit(), exit() */
  15. #include <unistd.h> /* read() */
  16. #include <stdio.h> /* printf() */
  17. #include <string.h> /* memcpy */
  18. static struct termios g_old_kbd_mode;
  19. /*****************************************************************************
  20. *****************************************************************************/
  21. static void cooked(void)
  22. {
  23. tcsetattr(0, TCSANOW, &g_old_kbd_mode);
  24. }
  25. /*****************************************************************************
  26. *****************************************************************************/
  27. static void raw(void)
  28. {
  29. static char init;
  30. /**/
  31. struct termios new_kbd_mode;
  32. if(init)
  33. return;
  34. /* put keyboard (stdin, actually) in raw, unbuffered mode */
  35. tcgetattr(0, &g_old_kbd_mode);
  36. memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
  37. new_kbd_mode.c_lflag &= ~(ICANON /*| ECHO */ );
  38. new_kbd_mode.c_cc[VTIME] = 0;
  39. new_kbd_mode.c_cc[VMIN] = 1;
  40. tcsetattr(0, TCSANOW, &new_kbd_mode);
  41. /* when we exit, go back to normal, "cooked" mode */
  42. atexit(cooked);
  43. init = 1;
  44. }
  45. /*****************************************************************************
  46. *****************************************************************************/
  47. static int kbhit(void)
  48. {
  49. struct timeval timeout;
  50. fd_set read_handles;
  51. int status;
  52. raw();
  53. /* check stdin (fd 0) for activity */
  54. FD_ZERO(&read_handles);
  55. FD_SET(0, &read_handles);
  56. timeout.tv_sec = timeout.tv_usec = 0;
  57. status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
  58. if(status < 0)
  59. {
  60. printf("select() failed in kbhit()\n");
  61. exit(1);
  62. }
  63. return status;
  64. }
  65. /*****************************************************************************
  66. *****************************************************************************/
  67. static int getch(void)
  68. {
  69. unsigned char temp;
  70. raw();
  71. /* stdin = fd 0 */
  72. if(read(0, &temp, 1) != 1)
  73. return 0;
  74. return temp;
  75. }
  76. #endif
粤ICP备19079148号