Contents / Part 2 / Previous chapter / Next chapter / Index


12 GET, INKEY

Sometimes it is useful to be able to detect a key as soon as it is pressed without having to wait for the RETURN key to be pressed. For example most games react immediately a key is pressed. There are a group of four functions which respond to single keystrokes:

GET

GET$

INKEY

INKEY$

The GET and GET$ functions wait until a key is pressed whereas the INKEY and INKEY$ pair give up after a while if no key is pressed.

100 A$ = GET$

will wait (for ever) until a key is pressed whereas

100 A$=INKEY$(200)

will wait for only 2 seconds (200 hundredths of a second). If no key is pressed within 2 seconds then the computer will move on to the next line of the program and A$ will be empty. If a key was pressed after say one second then the computer will immediately move on to the next line of the program and will put the "character typed" into A$.

100 PRINT "DO YOU WANT TO GO ON"

110 PRINT "YOU HAVE 2 SECONDS To REPLY"

120 A$=INKEY$(200)

130 IF A$="" THEN PRINT "TOO LATE YOU MISSED IT"

140 IF A$="Y" THEN PRINT "COURAGEOUS FOOL!"

150 IF A$="N" THEN PRINT "COWARD"

One of the most common uses of GET$ is to wait at the bottom of a page for a person to press any key when they are ready to go on

100 A$=GET$

GET and INKEY are very similar to GET$ and INKEY$ but instead of returning a character which can be put into a string variable they return a number which is the ASCII code of the character. The ASCII code of "Y" is 89 and the ASCII code of "N" is 78, so the last program could be re-written as

100 PRINT "DO YOU WANT TO GO ON"

110 PRINT "YOU HAVE 2 SECONDS TO REPLY"

120 A=INKEY(200)

130 IF A=- 1 THEN PRINT "TOO LATE YOU MISSED IT"

140 IF A=89 THEN PRINT "COURAGEOUS FOOL!"

150 IF A=78 THEN PRINT "COWARD"

You will see that "no reply" returns the value -1 when using INKEY and returns an empty string when using INKEY$.

Advanced features

Another important use of INKEY and GET is with the group of four direction keys at the top of the keyboard. Normally these are used for editing but a special statement can make these keys produce ASCII codes like all the other keys on the keyboard. They can then be used by a program for some special purpose - for example to move a point around the screen. The statement *FX 4,1 makes the editing keys produce ASCII codes and the statement *FX 4,0 returns the keys to their editing function. The keys produce the following codes

COPY		135 or (&87)
Left		136 or (&88)
Right		137 or (&89)
Down		138 or (&8A)
Up		139 or (&8B)

For example:

10 *FX 4,1

20 MODE 4

30 X=500

40 Y=500

50 REPEAT

60 PLOT 69,X,Y

70 K=GET

80 IF K=136 THEN X=X-4

90 IF K=137 THEN X=X+4

100 IF K=138 THEN Y=Y-4

110 IF K=139 THEN Y=Y+4

120 UNTIL Y=0

130 *FX 4,0

This program waits at line 70 for a key to be pressed. The program shown above would often be part of a much larger program in which case one would not want everything to stop until a key is pressed. Here it would be better to use

K=INKEY(0) at line 70 which will let the computer have a quick look to see if a key has been pressed but not wait at all.

10 *FX 4, 1

20 MODE 4

30 X=500

40 Y=500

50 REPEAT

60 PLOT 69,X,Y

70 K=INKEY(0)

80 IF K=136 THEN X=X-4

90 IF K=137 THEN X=X+4

100 IF K=138 THEN Y=Y-4

110 IF K=139 THEN Y=Y+4

120 UNTIL Y=0

130 *FX 4,0

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001