Key Reader (New Owners Column, Lesson 8)

Type
Education
Category
Beginner's BASIC lesson program (INPUT, OPEN and GET)
Author
David Plotkin
Published in
Antic, November 1986 (Vol. 5, No. 7), New Owners Column, Lesson 8: INPUT and DATA Statements
Year
1986
Language
Atari BASIC
Requirements
Any Atari 8-bit of any memory size, Atari BASIC; disk or cassette
GRAPHICS 0 text screen: PRESS ANY KEY AND I WILL TELL YOU WHICH KEY YOU PRESSED, followed by lines such as YOU PRESSED A : CODE 65, TAB : CODE 127, ESC : CODE 27, RETURN : CODE 155, BACKSPACE : CODE 126, a right-arrow symbol : CODE 31 and INSERT : CODE…

About the program

In November 1986, David Plotkin's New Owners Column turned to how a program gets information. Some is built into the program as DATA, read with READ and redirected with RESTORE. The rest comes from the person at the keyboard. The lesson covers the rules for DATA lines (nothing may follow them on the line, not even a REM), how READ takes items in order, how RESTORE to a line without DATA moves on to the next line that has some, and the errors you get from running out of DATA or reading a word into a number. It then compares INPUT, which waits for RETURN, with opening the keyboard as a device (OPEN #1,4,0,"K:") and using GET to read a single keystroke.

The listing puts the keyboard part into practice. It asks your name and greets you with a short typing animation. Then it becomes a handy tool: press any key, or a combination with SHIFT, CONTROL or the inverse key, and it tells you the key's ATASCII code. Plotkin encouraged readers to experiment, because these values "will be important in future columns".

The numbering of this series is a little confusing. The article is headed Lesson 8, but the disk file is NEWOWN7.BAS and the REM says "Part 7". In January 1987, Antic explained that October's "Great GOTO Debate" had been miscounted as a lesson, though the February 1987 column still calls this one Lesson 8.

Typing it in

Plain BASIC, with one inverse word ({inv}RETURN{/inv} in line 120: type it with the Atari key). The magazine version carried TYPO II checksums, which aren't in this disk version.

How to use it

Type your name and press RETURN. After the greeting, press RETURN again. Now press keys and the codes appear, one per line. Editing keys are named, for example TAB (127), ESC (27), BACKSPACE (126), SHIFT-DELETE (156), SHIFT-INSERT (157), CLEAR (125) and RETURN (155). The arrow keys show their arrow symbol. The program runs until you press BREAK, which GET never sees.

How it works

  • Drawing with characters. Line 80 draws the box round the greeting in the text screen with COLOR 42 and PLOT/DRAWTO. In GRAPHICS 0, the "colour" is a character code, and 42 is the asterisk.
  • Building the greeting. Line 90 puts "HELLO " into DUM$, adds the name at LEN(DUM$)+1, and centres the result. Line 100 prints it one letter at a time while a short rising sound plays.
  • Waiting for one particular key. Line 140 loops on GET #1,A until A is 155, the RETURN code.
  • Showing the unshowable. Printing an arrow key's code would move the cursor instead of showing anything. So lines 300–330 put an ESC character (27) in front of it, and the screen editor then displays the arrow as a symbol. Other special keys get a name from the chain of IF tests in lines 190–290. Everything else is shown with CHR$(A).

Variations to try

Add names for the remaining control keys, show the code in hexadecimal as well, or keep a list of the keys pressed and print them all at the end.

Listing

10 REM THE NEW OWNER'S COLUMN, PART 7
20 REM BY DAVID PLOTKIN
30 REM (c) 1986, ANTIC PUBLISHING
50 GRAPHICS 0:DIM NAME$(20),TXT$(15),DUM$(30)
60 POSITION 4,9:PRINT "WHAT IS YOUR NAME";:INPUT NAME$:REM THE INPUT STATEMENT
70 POKE 752,1:POSITION 0,10:PRINT " ";
80 COLOR 42:PLOT 4,14:DRAWTO 36,14:DRAWTO 36,18:DRAWTO 4,18:DRAWTO 4,14:DRAWTO 4,16:DRAWTO 5,16
90 LN=6+LEN(NAME$):LQ=20-LN/2:DUM$="HELLO ":DUM$(LEN(DUM$)+1)=NAME$
100 FOR LOOP=1 TO LN:POSITION LQ+LOOP-1,16:PRINT DUM$(LOOP,LOOP);
110 FOR WAIT=10 TO 20:SOUND 0,WAIT*5,10,4:NEXT WAIT:NEXT LOOP:SOUND 0,0,0,0
120 POSITION 4,20:PRINT "PRESS {inv}RETURN{/inv} TO CONTINUE"
130 OPEN #1,4,0,"K:"
140 GET #1,A:IF A<>155 THEN 140
150 PRINT CHR$(125):POSITION 3,2
160 PRINT "PRESS ANY KEY AND I WILL TELL YOU     WHICH KEY YOU PRESSED"
170 REM TEST FOR KEYS WHICH ARE CURSOR CONTROLS
180 GET #1,A:TXT$=""
190 IF A=27 THEN TXT$="ESC":GOTO 350
200 IF A=127 THEN TXT$="TAB":GOTO 350
210 IF A=159 THEN TXT$="SET":GOTO 350
220 IF A=158 THEN TXT$="CLR":GOTO 350
230 IF A=126 THEN TXT$="BACKSPACE":GOTO 350
240 IF A=156 THEN TXT$="DELETE":GOTO 350
250 IF A=254 THEN TXT$="CTRL-DELETE":GOTO 350
260 IF A=157 THEN TXT$="INSERT":GOTO 350
270 IF A=255 THEN TXT$="CNTRL-INSERT":GOTO 350
280 IF A=125 THEN TXT$="CLEAR":GOTO 350
290 IF A=155 THEN TXT$="RETURN":GOTO 350
300 IF A=31 THEN TXT$(1,1)=CHR$(27):TXT$(2,2)=CHR$(31):GOTO 350
310 IF A=30 THEN TXT$(1,1)=CHR$(27):TXT$(2,2)=CHR$(30):GOTO 350
320 IF A=28 THEN TXT$(1,1)=CHR$(27):TXT$(2,2)=CHR$(28):GOTO 350
330 IF A=29 THEN TXT$(1,1)=CHR$(27):TXT$(2,2)=CHR$(29):GOTO 350
340 TXT$=CHR$(A)
350 PRINT "YOU PRESSED ";TXT$;" : CODE ";A
360 GOTO 180
370 END

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: NEWOWN7.BAS.

Credits