• Advertisement
Sign in to follow this  

Capturing console input w/o breaking loop - c++

This topic is 4322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Been playing around with a console version of pong using no outside api's. I found out about _getch(), which allowed me to accept input without the wait for return, but now my loop is waiting for input. Is there a standard C++ command to catch keystrokes?
//psuedo

do
    draw(Ball);

    if(Keypress)
    {    
       int _getch();
       validate(int);
       draw(Paddle, int);
    }
    
    draw(AI, ballposition)
    Sleep(35);
while(input != 'Q')



As it stands I have no way to if(Keypress). So _getch just sits and waits for some input to occur and the rest of my loop waits with it, the ball doesn't move, the ai doesn't move unless you're moving your paddle. I've found tons of stuff for DirectInput, GDI, SDL, but I don't want to move into more advanced learning until I get the basics down. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
that type of input is platform dependant. you'll have to use an api or write your own os/hardware specific functionality.

Share this post


Link to post
Share on other sites
_kbhit() is exactly what you want. If it returns a non-zero value, that means a key was pressed, in which case you could use _getch() to read it.


int ch = 0;
if( _kbhit() )
ch = _getch();

if( ch != 0 )
HandleKey( ch );


Check this page:
Console level I/O

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement