INKEY$, CODE, CHR$, IF, THEN, GO TO, RESTORE, READ, DATA, PRINT, LET, STRING CONCATENATION, LOOPS, RECURSION
AN UPDATE TUTORIAL
My, My! I hope that the convoluted title doesn’t scare you off. If you are dazzled by alla them terms, just take a peek at the tiny listing at the end of this article, which uses all of the key words listed in the title. Then you will see that the whole bite wont choke you. All we’re gonna do is to get familiar with how to use these key words together to make the computer do something useful. That’s what PROGRAMMING is all about, and it ain’t too tough to compresend, even for a Florida cracker like me, who mixes them particycles and splits them infinitudes with regularity alla time. (Can’t spell real good, neither).
These platitudes will fit just about any computer, but with slight differences in syntax. The listing is in QL BASIC, but I’ll kinda zap in the changes for the TS-2068 as we go. AND, there’s better ways of doing it but there ain’t none thats no more simplier.
Now what the little listing called “inkey” does is to read the keyboard, display what is typed, and collects everything typed into the simple STRING “A$”, so’s you can do something else with the prose iff’n yer inclined to do so. But the real purpose is to getcha interested in PROGRAMMING so that you can eventually construct little utilities to SAVE to mdv or disk to use for many of yer own purposes. One thing that I learn’t back durin the Z-80 days was that keying in utility program lines is the best way to learn it durn it. Then experimenting with other ways to dun it is even more better.
Now a good way to make this epistle even more edgikationshul is to look up each of the key words used in the “Key Words” section of the user manual. Each one of them KEY WORDS has broader application than used for in this tutorial. Now lets get started with the explanations.
Line 100 and 110 clears the screen and then “initializes A$” as “nothing”, but ready to be assigned characters.
Line 120 assigns “variable A” to the code number of a character being read from the keyboard. “INKEY$” makes the computer do the keyboard reading job. TS-2068 users, just leave off the brackets inclosing INKEY$. Each character has an “ASCII CODE number”, like the character 0 (zero) is 48, and the space character is 32, and the “z” character is 122. Just to prove that, type: PRINT CHR$ (z). Now type: PRINT CODE (122). See how CODE and CHR$ correlates? So, if we say LET A=CODE (INKEY$), as we do in line 120, then we are saying “assign the ASCII CODE number of the KEY STRUCK to variable a”. Now what happens is that the computer sits there and reads the keyboard about a jillion times a second, and every time it does it assigns a number to variable a. It works the hell out of “a” but we dont care bout that. Everytime it reads the keyboard, the computer goes on to line 130 and beyond.
Line 130 has “IF THEN” conditional logic. We want the computer, while it is reading the keyboard, to “keep on reading it” IF the KEY STRUCK is not one of the PRINTABLE characters. Remember that “variable A” contains the code of the keyboard character. Code zero is “no key”, and we dont want any character LESS than 32 (space) or more than 122 (z) to be accepted. So, line 130 tells the computer to GO BACK to reading the keyboard IF the key struck isn’t in the range of “space (32) to z (122)”. If you like names, this is called “recursive programming”, which means that you have created a loop back to keep on doing something. In this case it is to line 120.
Line 140 does a similiar task, but in this case we provide an ESCAPE from the keyboard reading. IF the key struck is “#” THEN we make the computer GOTO line 200 to end the action. So, your escape from keyboard typing is the “#” key.
Line 150 is a little harder to understand. RESTORE is a command that tells the computer WHERE to “START READING a DATA LINE”, and to “READ the FIRST piece of data in that line”. WHAT LINE? In this case we dont specify the line number, which tells the computer to READ the FIRST DATA LINE in the WHOLE program, no matter where it is. Line 150 would work just as well if we make it read 150 RESTORE 200. We could make a READ command start at any DATA line by specifying the line number in the RESTORE line. Example, “150 RESTORE 300” would make a READ command start READING DATA pieces at line 300, “even if there were several DATA lines ahead of 300”. The commands “RESTORE, READ, and DATA” work together as syrup, butter, and pancakes. AND, any of the three commands used REQUIRE the other two to exist.
Line 160 tells the computer to READ the DATA line specified by the RESTORE in line 150. Since line 150 did not specify a line, then the FIRST DATA LINE is the one to read, which is line 200. At line 200 there is only one piece of data, and that is variable A. Variable A contains the keyboard character code (assigned back at line 120). So, each time a key is struck, line 160 picks it up with the READ command. It is significant to note that RESTORE is used at line 150 “after each keyboard read”. This starts the READ over at the beginning of line 200 to read the single number in variable “A”. Otherwise, if RESTORE was not encountered, an END OF DATA report would stop the program after the first READ.
Line 170 takes the character code that was READ at line 160 and converts the number to a character, and prints it. The semicolon after keeps the next print position on screen consecutively, instead of skipping down a line, as would be the case without the semicolon.
Line 180 adds each character generated at the keyboard to A$. Then A$ gets larger and larger as characters are typed. For TS-2068 users, replace the “&” with “+”. Such adding of characters onto an existing STRING is called STRING CONCATENATION. Try this in the direct mode: LET A$=”I Love ” ENTER. Then: PRINT A$ ENTER. Then: LET A$=A$ & “this!” ENTER. Then: PRINT A$ ENTER. Incidenally, some computers wont allow a simple string (A$) or a dimensioned string < DIM A$ (5,255) > contain more than 255 characters. Sinclair computers allow us to store an almost unlimited number of characters in STRINGS. This makes it very useful to use STRINGS and Character Arrays as DATA BASES.
Line 190: The GO TO makes the computer go back to pick up the next key struck. This is called a “loop back”, or “recursive programming”. We could use a “REPeat loop” here with the QL instead, but simple basic is just as good for this purpose.
line 210 is the beginning of the “loop ESCAPE” routine. Back at line 140, if the “#” key is struck, the computer is told to go to line 210. At line 210 we Clear the Screen with CLS. Then ‘PRINT A$’ prints the collected A$ data to the cleared screen. Each of the two back slashes “\” causes a line space on screen. The COLON after CLS lets us add another keyword for the computer to execute, instead of using another program line.
The loop escape routine at lines 140 and 210 allows us to construct other routines to do such things as EDIT the data, save it to mdv, or whatever we need to do with it.
Tutorial “inkey$”
100 CLS
110 LET a$=""
120 LET a=CODE (INKEY$)
130 IF a=0 OR a<32 OR a>122 THEN GO TO 120
140 IF a=CODE("#") THEN GO TO 210
150 RESTORE
160 READ a
170 PRINT CHR$(a);
180 LET a$=a$ & CHR$(a)
190 GO TO 120
200 DATA a
210 CLS: PRINT A$\
220 PRINT "Construct what to do at line 200"
230 STOP
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.