Up to now, we've been using INPUT to get things from
the keyboard. The problem with INPUT is that our program
stops until the user presses the enter key. Wouldn't it
be nice to keep the program running and still be able to
get input from the keyboard? INKEY$ will let
you do this. Using INKEY$ is very important if you want
to make "real-time" game programs.
Let's fix the clock program to let the user press any
key to stop the program. This way the user doesn't have
to know about the Break key.
CLS LOCATE 3, 1 PRINT "Press any key to exit" DO LOCATE 1, 1 PRINT TIME$ SLEEP 1 LOOP WHILE INKEY$ = ""
Not bad at all. Now we don't need to teach the user
about the Break key. We can do the same thing in any of
our other programs that need the Break key. If the user
does not press a key, INKEY$ returns nothing or "".
This next program will sit in a loop getting keys from
the keyboard with INKEY$ and printing them to the screen:
CLS DO Key$ = INKEY$ IF Key$ <> "" THEN PRINT Key$; END IF LOOP
That little program can be used to find the various
secret codes used by INKEY$ to let you know the arrow
keys have been pressed. This is very useful in game programming
where the arrow keys might control a player in a game.
If you press an arrow key, you'll see that a space and
a letter are generated.
Source: http://jpsor.ucoz.com |