Loop until keypress? How to detect keystroke? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Loop until keypress? How to detect keystroke?

I am currently working on a random number generator for a class project.

I am having no problem with the random number generation part. However there

is one program requirement that I am having a problem with. The program

must loop until a user presses a key, upon which time the current random

number is then displayed. Then when the user presses a key again the program

must restart the loop again. I'm assuming he want's us to use an EOF to

end the program. I'm pretty sure that I'll use a while loop to fulfill the

looping.



Here is the question though: Is there a specific function in a library

to detect an event such as a keypress or can anyone give me a push in the

right direction i.e. do I have to write my own function? (I don't need a

complete solution, I can figure it out on my own if I just have an idea where

to look.) I'm using book by Deitel called C++ Learning to Program and I just

can't seem to find anything regarding the eventhandlers if thats even what

they are called in C++.


Comments

  • The _kbhit() function returns a 1 (true) when a key has been pressed, after that you need to actually get the key pressed from the keyboard buffer or any subsequent _kbhit() tests will still return 1 (true). You can do something like this:



    char cTestChar = ' ';

    while( cTestChar != 'z' && cTestChar != 'Z' )

    {

    while( !_kbhit() ) cout << "Hello" << endl;<br>
    cTestChar = getch();

    cout << "You pressed " << cTestChar<br>
    << '.' << endl;<br>
    while( !_kbhit() );

    cTestChar = getch();

    }



    This keeps on spitting out Hello until you press a key at which point the program stops a bit and tells you what key you pressed. Once you again press a key, the program goes back to spitting out "Hello" again unless the second key pressed was a 'z' or 'Z'. I tried this under MS Visual Studio 6.0 and it worked fine. You need to include conio.h to get _kbhit() to work.


  • Thanks for you help but unfotunately I think the _kbhit function is only included in M$ Visual C++. At least I don't think it is in Borland 4.0 because I get errors saying I'm "calling an undefined funciton _kbhit" even with conio.h included. It looks like that would have worked perectly had the function been included. Thanks for you help anyways though.








  • Visual C++ and Borland C++ both use slightly different funstion names for identical functions.



    In Visual C++, it's the function _kbhit().

    In Borland C++, it's simply the function kbhit(),

    you'll have to include the conio.h for this to work.



    -Psycho-


  • Ahh that's the problem. Thanks alot to both of you for your help. I was starting to pull my hair out. hehe.



    : Visual C++ and Borland C++ both use slightly different funstion names for identical functions.

    :

    : In Visual C++, it's the function _kbhit().

    : In Borland C++, it's simply the function kbhit(),

    : you'll have to include the conio.h for this to work.

    :

    : -Psycho-

    :






Sign In or Register to comment.