Borland Extensions

<conio.h>

Quick model of the keyboard buffer

Whenever a key is depressed the code for that key is placed in the keyboard buffer. When that code is processed, it is removed from the buffer. So you can think of the keyboard buffer as being a pipeline full of all of the keystrokes that haven't been taken care of yet.

int kbhit(void);

This function doesn't take any arguments and returns 0 (False) if the the keyboard buffer is empty and returns something other than zero (True) if the keyboard buffer has anything in it.

Note that this function does not alter the contents of the buffer - it only tests to see if it is empty or not. 

int getch(void);

This function actually removes a character from the keyboard buffer and returns the ASCII code of that character.

int putch(int c);

This function prints the character whose ASCII code is c at the present cursor location of the display device. 

The return value, which is seldom checked or used, is simply the character that was printed if the operation was successful or EOF (a constant defined in the standard library files) if it was not.

void gotoxy(int col, int row);

This function places the cursor at the row and column that are passed to it. Nothing is actually printed. The next character that is printed will be printed at that location - unless some other instruction moves the cursor, of course.

The console display (the screen) is a window that has:

Example:

    gotoxy(13,20);

    putchar('y');

Would print the letter y at row 13 column 20.

Pausing program execution until a line is hit

Consider the following code:

    while(!kbhit());

This empty loop actually serves a useful purpose - it stalls the program and holds it in this loop as long as the keyboard buffer is empty - since kbhit() will be False, !kbhit() is True. As soon as a key is pressed, the test will fail and you'll drop out of the loop.

But note that checking to see if the buffer is empty does nothing to the contents of the buffer, so if you were to run this loop a second time it would fail immediately since the same character that caused it to drop out of the prior loop is still sitting there in the buffer.

The simplest way to remove a character from the keyboard buffer is to use the function getch() which returns the ASCII code for the first character in the keyboard buffer and removes that character from the buffer. Notice that it doesn't clear the buffer - it removes only the first character.

To clear out the keyboard buffer, you want to have a loop that asks if the buffer is empty and, if not, remove a character from it. Then check again and continue with this process until the buffer is empty. So the following code will accomplish this:

    while(kbhit())

        getch();

Notice that we are not using the value returned by getch(). We could and in many cases would. But if all we want is to detect when a key has been pressed, this will do fine.

So what if we want to pause and tell the user to hit a key to continue? The first thing to consider is that there may already be something in the keyboard buffer before we even get to this point - perhaps the user hit a few keys while the program was running earlier code or perhaps the previous code didn't clear the buffer. So we should go ahead and clear the buffer first. Then we can sit in our loop waiting for something to be placed in the buffer and, finally, we should perform some housekeeping and clear out the buffer so as not to leave our trash lying around to mess up later code.

  // WAIT FOR ANY KEY --------------------------- 

   while(kbhit())    // Clear Keyboard Buffer

      getch();

   while(!kbhit());  // Trap while Buffer is empty

   while(kbhit())    // Re-clear the buffer

        getch();

   //---------------------------------------------