Created
October 20, 2021 13:55
-
-
Save mschrader15/1417b802475abe5141590f447917ac05 to your computer and use it in GitHub Desktop.
khbit alternative for MacOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://cboard.cprogramming.com/c-programming/178177-very-simple-program-impossible-create-mac.html | |
// May need to clear input buffer between khbit() calls. Use fseek(stdin,0,SEEK_END); | |
#ifndef GETCH_H_INCLUDED | |
#define GETCH_H_INCLUDED | |
#include <stdio.h> | |
#include <termios.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
int ch; | |
unsigned long wait=0; | |
int kbhit(void) | |
{ | |
struct termios oldt, newt; | |
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; | |
} | |
#endif // GETCH_H_INCLUDED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment