Keyboard Techniques

Les Ellingham

 

Issue 3

May/Jun 83

Next Article >>

<< Prev Article

 

There are two commonly used methods of obtaining input from the keyboard during a program. The first is the INPUT statement which can be used to input either a numeric variable or a string and the second is the GET statement which accepts only one keystroke in the form of an ATASCII number. Appendix C of the Basic Reference Manual gives a full list of the ATASCII character set and corresponding decimal codes.

One thing both these statements have in common is that they stop the program until such time as a key is pressed and in the case of the INPUT statement, until RETURN is pressed. In certain cases this can be a distinct disadvantage. You may, for instance, wish to set a time limit in which to enter an answer to a question or may wish to have a 'real-time' battle scene giving you only seconds in which to attack or defend. For both these situations neither INPUT or GET are of any use. Fortunately there is an answer and that is to take a PEEK at the keyboard. Memory location 764 holds a value which corresponds to the last key pressed or contains the value 255 if no key is pressed. So all you have to do is PEEK location 764, read the ATASCII code and convert that to see which key you pressed. Right? No, wrong! Unfortunately, the value stored in location 764 has no relation whatever to the ATASCII code and is an internal code recognised only by your Atari.

You can easily program around this limitation by combining a PEEK to location 764 with a GET statement, but in many cases there is no need to print out the character of the key pressed and it would be helpful to be able to include the keyboard code in your program for immediate recognition. The Basic Reference Manual does not include a list of the keyboard codes and I have not seen them printed elsewhere, so the accompanying chart lists the internal code of all the keys.

Fine, another chart to refer to, but what practical use can this be put to? I mentioned earlier a 'real-time' battle scene where you had to input information quickly to attack or defend.

When attacking, you may, for instance, wish to hit 'H' for an attack to your opponent's head, 'B' for an attack to the body or 'V'' for an attack to the legs. The following subroutine can be used.

100 POKE 764,255:FOR I=1 TO 20

110 IF PEEK(764)=57 THEN GOTO 500

120 IF PEEK(764)=21 THEN GOTO 600

130 IF PEEK(764)=0 THEN GOTO 700

140 NEXT I:? "OUT OF TIME!"

Line 500 would determine the outcome of a blow to your opponent's head, line 600 to his/ (its?) body and line 700 to the legs. The length of the loop should be altered to suit how quickly you wish to react. If you failed to hit any of the correct keys, the program would return from the subroutine to allow your opponent to continue his attack. If this type of subroutine is used for a 'surprise' attack, preceded only by a quick warning, it is surprising how nimble you have to be to hit the right keys!

If you wish to use location 764 to obtain an answer within a time limit, then again PEEK at the keyboard from within a loop. Suppose, for example, you have a program which poses a question and gives a choice of four answers - A, B, C or D. The following can be used as a subroutine (assume C is the right answer).

100 POKE 764,255:FOR I=1 TO 200

110 IF PEEK(764)=18 THEN ?"RIGHT":RETURN

120 IF PEEK(764)<>255 AND PEEK(764)<>18 THEN ?"WRONG":RETURN

130 NEXT I:?"TIME UP":RETURN

There must be many other ways to use the internal keyboard code and also routines which involve POKEing location 764. Certainly you can fool your Atari into thinking you have pressed RETURN by a POKE 764,12. When you next load a program try typing POKE 764,112:CLOAD (make sure that the tape is ready first!).

The secret of learning to program is to experiment. Try expanding the simple routines given and making up some of your own. The suggestions are only a couple of many uses for a PEEK at the keyboard. What other interesting applications can you find?

INTERNAL KEYBOARD CODES

KEY

CODE

 

 

1

31

2

30

3

26

4

24

5

29

6

27

7

51

8

53

9

48

0

50

A

63

B

21

C

18

D

58

E

42

F

56

G

61

H

57

I

13

J

1

K

5

L

0

M

37

N

35

O

8

P

10

Q

47

R

40

S

62

T

45

U

11

V

16

W

46

X

22

Y

43

Z

23

ESC

28

<

54

>

55

BACK S

52

TAB

44

-

14

=

15

RETURN

12

;

2

+

6

*

7

CAPS/LOW

60

,

32

.

34

/

38

 

39

SPACE

33

PRESSING SHIFT AT THE SAME TIME ADDS 64. PRESSING CTRL AT THE SAME TIME ADDS 128.

PRESSING SHIFT AND CTRL AT THE SAME TIME AS A KEY ADDS 192. BUT ONLY IF THE KEY VALUE IS 24 OR OVER.

top