Is there a portable version of kbhit()? Perhaps a windows function?
Printable View
Is there a portable version of kbhit()? Perhaps a windows function?
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
Don't you hate that?
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;
}
Is there an extremely low level way to do it maybe?
It is possible to use a BIOS interruption. But I'm not sure how portable it willt be :rolleyes:
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.
I'm sorry, I don't understand....
I've never gone into this stuff before; please explain.