ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

INKEY$ Function

Summary

If characters are waiting in the keyboard buffer, INKEY$ reads and returns the next character in the buffer. An empty string is returned if no characters are waiting in the buffer.

Syntax

string variable = INKEY$

Remarks and Examples

Keystrokes entered by the user that have not yet been consumed are collected in a list called the keyboard buffer.

The INKEY$ statement checks whether there are any keystrokes waiting in the keyboard buffer. If there is at least one character waiting to be processed, INKEY$ removes the next character from the buffer and returns a string containing that character.

If there are no characters waiting in the keyboard buffer, INKEY$ returns an empty string ("").

INKEY$ is a non-blocking function: it does not pause the program and wait for the user to press a key. INKEY$ is therefore suitable for loops where processing or animation takes place which can be interrupted or influenced by keystrokes entered by the user.

INKEY$ does not display the character on the screen.

To make the program wait until the user has pressed a key, use the GETCHAR$ function instead. To let the user enter a string or number, use the INPUT function.

Example program:

10 CHARTOPRINT$ = "*"
20 FOR T = 0 TO 5000 STEP .25
30 J$ = INKEY$ : REM Check whether the user pressed a key
40 IF J$ = "" THEN 60 : REM No key was pressed, so don't change the character to print
50 CHARTOPRINT$ = J$ : REM The user pressed a key. Change the character to print to the input character
60 A = INT(40+25*SIN(T))
70 FOR X = 1 TO A : PRINT " "; : NEXT X
80 FOR X = 1 TO 10 : PRINT CHARTOPRINT$; : NEXT X
90 PRINT
100 NEXT T
RUN
      **********
             **********
                  XXXXXXXXXX
                      XXXXXXXXXX
                        3333333333
                         3333333333
                        3333333333
                      QQQQQQQQQQ
                  QQQQQQQQQQ

(The program begins by printing asterisks; as the user presses keys, the typed characters are used for the animation instead of asterisks.)




Copyright 2006-2008, Kevin Matz, All Rights Reserved.