/* * * Mark Phythian */ #include #include char GetKeypress(void); int kbhit(void); int main() { char c = 0; int i = 0; // I think I have fixed this problem now. //note a getchar or GetKeypress call before kbhit stops kbhit from detecting keys //I have yet to find ot why or how to fix it but kbhit followed by GetKeypress is fine printf("Type a key \n"); fflush(stdout); while(c != 'q') { while(!kbhit()) { printf("waiting\t"); fflush(stdout); sleep(1); } c = GetKeypress(); printf("GetKeypress key was %c \n",c); fflush(stdout); } exit(0); } char GetKeypress( void ) { HANDLE hStdin; DWORD result; INPUT_RECORD inBuf; hStdin = GetStdHandle(STD_INPUT_HANDLE); while(1) { ReadConsoleInput(hStdin, &inBuf, 1, &result); if(inBuf.EventType == KEY_EVENT) { ReadConsoleInput(hStdin, &inBuf, 1, &result); return inBuf.Event.KeyEvent.uChar.AsciiChar; } } } int kbhit( void ) { HANDLE hStdin; DWORD result; INPUT_RECORD inBuf; hStdin = GetStdHandle(STD_INPUT_HANDLE); PeekConsoleInput(hStdin, &inBuf, 1, &result); if(inBuf.EventType == KEY_EVENT) return 1; return 0; }