Is there a portable version of kbhit()? Perhaps a windows function?
Is there a portable version of kbhit()? Perhaps a windows function?
"If you tell the truth, you don't have to remember anything"
-Mark Twain
If the function were specific to Windows then it wouldn't be portable, would it? I believe the API function GetKeyboardState() does what you want, but there's no truly portable way to get the functionality of kbhit.
-Prelude
My best code is written with the delete key.
If you really cannot live without this piece of cr*p function...then here's a Windows version that I just wrote.......
Code:void kbhit(void){ HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD ip;//Holds keyboard event data DWORD dwResult; while(1){ ReadConsoleInput(hIn,&ip,1,&dwResult);//Get event if(ip.EventType == KEY_EVENT)//Is it keyboard? break; } return; }
dont forget the windows.h
Using what Fordy gave me, I wrote this:
It does not, however, give the desired effect when I use it in a loop.Code:bool hitkb(void) { HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD ip;//Holds keyboard event data DWORD dwResult; ReadConsoleInput(hIn,&ip,1,&dwResult); if(ip.EventType == KEY_EVENT) return true; return false; }
Example:
This program displays 0 even if you never touch the keyboard.Code:int main() { int Frames; while(!hitkb()) { Frames++; } cout<<Frames; return 0; }
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Is there an extremely low level way to do it maybe?
"If you tell the truth, you don't have to remember anything"
-Mark Twain
It is possible to use a BIOS interruption. But I'm not sure how portable it willt be
At least PC x86 compatible.
kbhit() should be the same call of (low level, BIOS interruption):
if ZF = 0 (zero flag), a key was pressedCode:asm { mov ah, 1 int 16h }
kbhit() should return true is a key was pressed
Like kbhit(), the key code is not removed from keyboard buffer, using this call.
I could write using the regs struct, but cannot remember it and I dont know if it is portable.
Dharius
I'm sorry, I don't understand....
I've never gone into this stuff before; please explain.
"If you tell the truth, you don't have to remember anything"
-Mark Twain