Click here if you are stuck in someone else's frames.
Implementing Keyboard Control

Joysticks are nice for playing video games.  For most games, they are your best bet for best control in the fast action and responses.  However, a lot of PC's (in fact, a vast majority of them) do not offer joystick control and as we have discovered their interface is very weak which depends largely on software timing loops that are highly sensitive to the processing speed of the PC.  (On my old system in fact, the interface is unstable to the point to where it is practically useless.)  Therefore, we also need to write our games so that they can interface with the king of all input devices on the PC, and that is the keyboard.

Up to this point, we have actually written a few programs that makes use of the keyboard in some fashion.  Throughout this text, you've seen code such as this:

     while (kbhit())  getch();    /* Clear the keyboard */

and this:

     while (!kbhit());            /* Wait for a key */

Well, if you have done any kind of C programming before, you would undoubtedly recognize what these functions do.  The 'kbhit()' function checks the keyboard buffer to see if a key is in the keyboard buffer and 'getch()' gets the next character off the keyboard buffer and returns its scan or ASCII code.  Combining them can allow for fast action programs that don't cause the computer to stop and wait for a key to be pressed every time a key needs to be read.

Previous Page | Main Page | Next page

Send your questions, comments, or ideas here.