Chapter
10
I
BASIC
Keywords
INKEY$
Function
INKEY$
Reads
a
character in the keyboard buffer, and returns a 0-,
1-,
or
2-
byte string. INKEY$ does not echo the character
to
the display.
0
A
0-byte (null) string indicates that no key is pressed.
0
A 1-byte string is an actual character read from the keyboard.
0
A 2-byte string indicates that the key pressed is one
of
the special
keys that has an extended code. The first byte is hex 00. See Ap-
pendices
B
and
D
for
a
complete list of extended codes.
INKEY$ is invariably put inside some sort of loop. If not, pro-
gram execution passes through the line containing INKEY$ be-
fore you can press a key.
The
[CTRLI[m!
and
IHOLD]
keys are not passed
to
INKEY$. Also
[ALT][CTRLI[],
which does
a
system reset, is not passed
to
INKEY$.
Note:
If your program contains an INKEY$ and you
press a function key, BASIC returns
1
character
of
the
key assignment at a time. For example, suppose this
statement is executed:
A$
=
INKEYF
Now suppose you press
[,
which initially has the
value LIST. The first time the statement is executed
A$ equals
L,
the second time
A$
equals
I,
and
so
on.
Keep this in mind when writing a BASIC routine
to
trap for a certain key. Your routine may not perform
as expected if you accidently press a function key.
You can assign the result
of
INKEY$
to
a string variable and
test the length
of
the string
to
determine whether a 0-,
1-,
or
2-
character string
is
returned by
INKEY$.
Example:
10 A$=INKEY$:
IF
A$=""
THEN 10
20 IF LEN(A$)>l THEN PRINT ASC(MID(A$,l,I)),
30 GOT0 10
ASC(MID$(A$,2,1)) ELSE PRINT
ASCCA$)
Ex
ample
10
A$
=
INKEYS
20 IF
A$
=
"I'
THEN 10
causes the program
to
wait
for
you
to
press a key.
177