You are here:

C++/c++ graphics

Advertisement


Question
hi sir
act i have made simple program in c++ to move a circle left of right  by pressing 1 for left and 2 for right and i have to press enter after pressing 1 or 2
but how i will get d desire move without pressing enter

Answer
Hello Kitty.

There is a function called kbhit which will return non-zero if there is a character in the keyboard buffer. You can use it to see if the user pressed a key. Then you can use getch to read the character to see if it is a 1 or 2.

If there is no character in the keyboard buffer, kbhit will return 0.

The code would look something like this:

#include <conio.h>
void example(void)
{
   while(true)
   {
       if (_kbhit())
       {
           char ch = (char)_getch();
           if (ch == '1')
           {
               // Move circle right
           }
           else if (ch == '2')
           {
               // Move circle left
           }
       }
   }
}

On my system, the kbhit and getch functions start with an underscore character.

I hope that helps you.

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.