TIGCC Programming Lessons
Lesson 2 - Basic Keyboard Input
Step 3 - Alternative to ngetchx() 1: The kbhit() function
As we have seen in the previous example, the ngetchx() function waits
for the user to press a key before the program can continue. This is
often a big drawback, and something we need to avoid; so we need
some alternative form of handling keyboard input. Enter kbhit(). The
kbhit() function will tell us if a key has been pressed. So, if we
switch the condition in the previous example for the kbhit() function,
we can work the body of the loop while no keys have been pressed.
Start a new project in TIGCC with a C Source file named kbhit. Modify
the program so that is looks like this.
kbhit.c
#include <tigcclib.h>
void _main(void) {
int key, number = 0;
// clear the screen
clrscr();
// loop until user presses a key
while (!kbhit()) {
printf("%d\n", number); // print the number
++number; // increment the number
}
// grab the keycode and display it
key = ngetchx();
printf("You pressed keycode %d\n", key);
// wait for input before exiting
ngetchx();
}
Step 3a - Compile and Run the Program
Build the program and send it to TiEmu. It should look something like
this, depending upon what key you press and how long you wait to press
it.
Step 3b - Analyze the Program
This is another fairly straightforward program, but it never hurts to
undergo program analysis.
int key, number = 0;
// clear the screen
clrscr();
This is pretty simple. As we talked about in part 1, we can declare
multiple variables and assign values to them all on a single line.
// loop until user presses a key
while (!kbhit()) {
printf("%d\n", number); // print the number
++number; // increment the number
}
This is where the new segment comes in. We have another while loop, but
with a new condition this time. Instead of assigning key values from the
ngetchx() function, we check if a key has been hit. Just like in !=, the
! means "not". The kbhit() function returns true if a key has been
pressed; false otherwise. So we will stay in the while loop if no key
has been hit.
The loop body is simple, but has some nuances you may not have seen
before if you are new to C. First, we have a new kind of value in the
printf() function. The '%d' is called a format specifier. Format
specifiers are used in formatted strings (the f in printf means print
formatted) to stand in place of another value which will be substituted
later. The printf() function replaces these place holders with the
variables we specify as arguments to the function. The specific '%d'
specifier is used for int variables. So, %d means 'replace me with the
value of an integer'. So, this printf function prints the integer value
stored by the 'number' variable followed by a new line character.
The last statement in the loop body is the pre-increment statement. It
increases the value of the variable by 1.
// grab the keycode and display it
key = ngetchx();
printf("You pressed keycode %d\n", key);
// wait for input before exiting
ngetchx();
From the previous lessons and examples, these statements should be no
problem for you, but the surprise may be that ngetchx() does not have to
wait this time, because we already know the user pressed a key. This
was taken care of in the while condition. Since a key has already been
pressed, we can use ngetchx() to retrieve this value for us. Then we
just print out the value of the keycode to the screen.
As always, we wait for one more key press before exiting out program.
Step 3c - Programmatic Conclusions
This program demonstrates a different method of key reading, and can be
quite useful in programs that need to do other things while checking for
input. This will work fine for simple games that don't need a ton of
speed, but you will need faster methods for real speed.
In the final part of the lesson, we will learn about the fastest
built-in method of keyboard reading.
Continue with Part III
|