Qbasic Tutorial for beginners and children (Under Construction)


Getting user data at run-time (continued)

The INKEY$ function

 

The INKEY$ function reads a character from the keyboard. Its main advantage over the INPUT statement is that it does not require the user to press the ENTER ( ) key. It is thus more appropriate in games.

The program below displays a sentence repeatedly until the Escape (Esc) key is pressed. 

DO  

    PRINT "Press the escape (Esc) key to stop!"

LOOP UNTIL INKEY$ = CHR$(27)

 

Example 1: Choosing between two options

Program Code

PRINT "1. Play Hangman"
PRINT "2. Play Tetris"

PRINT "Enter your choice:"
DO
  a$ = INKEY$
LOOP UNTIL a$ = "1" OR a$ = "2"
IF a$ = "1" THEN
  PRINT "You have chosen to play Hangman."
END IF
IF a$ = "2" THEN
  PRINT "You have chosen to play Tetris."
END IF

Output on Screen

In the program test below the user has pressed number "1"

1. Play Hangman
2. Play Tetris

Enter your choice:
You have chosen to play Hangman.

 

 

The INKEY$ function is more appropriate in menu options and in games such as Hangman, Tetris and other games where continuous input has to be monitored. See also: A simple drawing program

[ Previous Index Next ]

 


pages viewed since January  19 2002