kbhit() portability

This is a discussion on kbhit() portability within the C++ Programming forums, part of the General Programming Boards category; Is there a portable version of kbhit()? Perhaps a windows function?...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    kbhit() portability

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Don't you hate that?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Using what Fordy gave me, I wrote this:
    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;
    }
    It does not, however, give the desired effect when I use it in a loop.
    Example:
    Code:
    int main()
    {
    int Frames;
    while(!hitkb())
    {
    Frames++;
    }
    cout<<Frames;
    return 0;
    }
    This program displays 0 even if you never touch the keyboard.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    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):
    Code:
    asm
    {
       mov ah, 1
       int 16h
    }
    if ZF = 0 (zero flag), a key was pressed
    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

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-30-2007, 08:46 AM
  2. implicit declaration of int kbhit(...);
    By Blizzarddog in forum C++ Programming
    Replies: 14
    Last Post: 11-13-2003, 04:39 PM
  3. Program Portability (code portability)
    By Perica in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2002, 09:03 AM
  4. Pausing for input, with kbhit() still detecting keys
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 05:04 AM
  5. need more help on kbhit()
    By lordvlaad() in forum C++ Programming
    Replies: 0
    Last Post: 04-20-2002, 02:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21