Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4569 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?


       int _getch();
       draw(Paddle, int);
    draw(AI, ballposition)
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
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

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!