# Turbo C++



## motocross95 (Oct 30, 2001)

I am using Turbo C++ and I am making a simple program that displays circles and random X and Y coordinates and random colors. One problem, I'm not sure how to perform a loop in Turbo C++ until a key is pressed. I tried something like 

do
{
//Code goes here
} while (getch() != "");

As you can guess it does not work, I get an error. I also just made it....while (getch()); But that just makes it pause after every loop until you press a key. Any help would be greatly appreciated thank you.


----------



## MacFromOK (Mar 19, 2001)

Here ya go. 

```
while(!kbhit())  // Loop until user hits a key.
  { // Code goes here.
  }
if(getch() == 0) // Dispose of kbhit character if not needed.
   getch();
```


----------



## motocross95 (Oct 30, 2001)

Thank you MacFromOK, it worked without any errors. Just one more question now. I'm trying to gather enough knowledge to eventually try making PONG. So eventually I'm gonna have problems with collission detection if anyone cares to elaborate on that now, but that question now is.....After it exits the loop how do I tell what key you pressed? as in LEFT, RIGHT, DOWN, UP, or Esc keys? I know escape is Chr$(27) in qbasic. Because I need to run a check and perform the correct operations depending on it. Thank you in advance


----------



## MacFromOK (Mar 19, 2001)

Here's a little prog I wrote a long time ago. It should
give you an idea how to check for the scan code, as
well as being a handy tool to find them. 


```
#include &ltconio.h>
#include &ltstdio.h>

void main()
{
  int keyhit = 0;

  printf("\nKEYTEST by Mac. Displays scan code of key pressed. ESC to exit.\n\n");
  while(keyhit != 27)
    { keyhit = getch();
      if(keyhit > 0)
        printf("%c scan code = %d", keyhit, keyhit);
      if(keyhit == 8)
        printf(" (Backspace)");
      if(keyhit == 9)
        printf(" (Tab)");
      if(keyhit == 13)
        printf(" (Enter)");
      if(keyhit == 27)
        printf(" (Esc)");
      if(keyhit == 32)
        printf(" (Spacebar)");
      if(keyhit == 73)
        printf(" (Page Up)");
      if(keyhit == 81)
        printf(" (Page Down)");
      if(keyhit == 71)
        printf(" (Home)");
      if(keyhit == 79)
        printf(" (End)");
      if(keyhit == 82)
        printf(" (Insert)");
      if(keyhit == 83)
        printf(" (Delete)");
      if(keyhit > 0)
        printf("\n");
    }
}
```


----------



## motocross95 (Oct 30, 2001)

That is a nice program thank you, it will be very helpful. I have a question. Run that program and tell me what it says when you type a capital S. notice it is the same as the delete key? I find that weird. It happens with other buttons as well, Insert, Home, Page Up, Delete, End, Page Down. Have any ideas as to why it does that? Thank you again for that nifty little program.


----------



## MacFromOK (Mar 19, 2001)

You are correct. That's why I put the "Insert",
"Delete", etc. in parenthesis. They are the
same as some capital letters. I don't know
why, but they are.

Basically, this just means that the same
getch() procedure will catch either keyhit.

Cheers, Mac 

[EDIT]
The left/right and up/down arrow buttons
also share scan codes with capital letters...


----------

