Classic Computer Magazine Archive COMPUTE! ISSUE 65 / OCTOBER 1985 / PAGE 84

Dynamic Keyboard For
Commodore Machines
Part 1

Jim Bufferfield, Associate Editor

Dynamic keyboard techniques let you perform tasks that would otherwise be difficult or impossible in BASIC. The first article in this two-part series covers the fundamentals. In Part 2, we'll look at more advanced uses of the dynamic keyboard.

Many BASIC commands can be used in either direct mode (typed directly on the keyboard without a line number) or program mode (as part of a program). Certain commands, however, work only in direct mode. Using them in a program requires the dynamic keyboard technique, which lets a program act like it's you-typing commands on the keyboard. This method is especially effective on Commodore machines because of their full-screen editing. The term dynamic keyboard was first used by Mike Louder in 1978, though the technique had been used previously by Larry Tessler to merge programs.

Direct Versus Programmed
A direct-mode command doesn't have a line number and is executed as soon as you press RETURN. An example is PRINT "HELLO". In program mode, the command does have a line number and is executed only when you type RUN and then press RETURN. An example is 10 PRINT "HELLO". Most BASIC commands work in both direct and program mode.
    A few BASIC commands cannot be used in direct mode, however; they may appear only in a program. GET, INPUT, GET#, and INPUT# are the best-known of these. Usually these commands use a segment of memory called the input buffer to store data as it arrives, and they won't work in direct mode because the same input buffer is used to hold the command itself. Thus, the incoming data might overwrite the command you typed in. An easy way to see this conflict is to use GOSUB as a direct command, calling a routine that does input. Try the following simple program:


    Execute this routine by typing GOSUB 300 and pressing RETURN. The subroutine will ask YOUR NAME?. If you reply with a one-character name, such as X, everything works fine. The RETURN takes you back to the keyboard, and the computer reports READY. But if you reply with a longer name such as CHARLOTTE, you may get a strange error message. Why? Your original command GOSUB 300 is still sitting in the input buffer. When the subroutine ends, the system looks beyond the GOSUB command to see what comes next. We expect it to find an end-of-command marker and quit. But the GOSUB command has been destroyed. It was overwritten by the name you typed in, which went to the same input buffer. The result is confusion.
    On the other hand, some BASIC commands can be used only in direct mode-not in a program. CONT, for example, causes an indefinite pause when used in a program. LIST works in program mode, but on most Commodore computers the program ends after executing LIST. In direct mode, you can enter a program line to add to the program or change it. You can't do this while running a program. Again, there's a difference between programs and direct commands - they have different powers.
    A very important difference is found in the LOAD command. If typed as a direct command, LOAD fills memory with a new program from tape or disk. If there was already a program in memory, it vanishes and its variables are thrown away. But a LOAD command executed within a program is quite different. The new program comes in, but existing variables are not scrapped-they are preserved so that the new program can use them. This is a powerful programming technique called chaining, which lets one program continue processing data that was generated by a previous program.

Invisible Fingers
Direct keyboard statements can perform certain tasks that programs can't (at least, not in the usual way). For example, if we want a program to invite a student to type in a formula, BASIC doesn't allow the formula to be evaluated (an INPUT statement won't evalute the formula 2 + 2 as 4).
    Similarly, suppose we want one program-perhaps a main menu program-to load and run another program. That's hard to do because BASIC wants to chain the new program to the old one. Instead of starting the next program fresh, it tries to make it a continuation of the previous program. On rare occasions, there may be a real need to allow a program to change itself, although this is tricky because every time you change a program (by editing a line, etc.), its variables go away. It's hard for any program to continue running after its variable values disappear.
    We can accomplish these things, however, by using a startling technique: making the computer type on its own keyboard. How can a computer do this? It doesn't even have any fingers.
    Here's how it works. When you strike a key, the information always goes first to a memory area called the keyboard buffer. After it gets there, it is picked up and used by the computer. If we can put a character in the keyboard buffer without actually pressing any keys, it will appear to have been typed, and the computer responds exactly as if the corresponding key was pressed.

Let's try a quick example to see how it works. The keyboard buffer is located in different places on different computers, so the commands must be tailored to the machine involved. We'll ask the machine to self-type the letter X:

For VIC-20 or Commodore 64:

POKE 198,1:POKE631,88

For Plus/4 or 16:

POKE 239,1:POKE 1319,88

For PET/CBM (3.0 and 4.0-BASIC):

POKE 158,1:POKE 623,88

For Original ROM PETs:

POKE 525,1:POKE 527,88

For Commodore B-128 (Model 700)

BANK 15:POKE 209,1:POKE 939,88

    The first POKE in each line tells the computer how many characters are waiting in the keyboard buffer. The second puts the character X in the first slot of the buffer. After you type the line and press RETURN, the computer reports READY and acts as if you pressed the X key. The letter X appears on the screen and the cursor flashes to its right. It would be easier just to type the X, of course, but we've established a new capability. A program can now, in effect, type on the keyboard.

Using The Screen
With this technique alone, you're limited to pretty short commands. The keyboard buffer usually has a size limit of about nine characters. Also, it's cumbersome for a program to put characters into the buffer one at a time. But on Commodore machines we can take advantage of screen editing to process longer direct commands.
    Whenever you press the RETURN key, the computer reads the screen. Whatever it finds there, it does-perform a command, enter a line, or whatever. To make a program execute a long direct-mode command, follow these steps:

1. PRINT the command on the screen in a known place.
2. Position the cursor a couple of lines above the command.
3. Put a carriage return in the keyboard buffer.
4. Terminate execution with an END command.

    When the program reaches END, here's what happens. The desired command is on the screen and the RETURN is in the keyboard buffer. The program terminates, and the computer prints READY. Although the program has ended, the computer receives the RETURN as if you had just pressed that key, so it executes the line on the screen. Among other things, that line might contain a GOTO or CONT that would continue the program.

A Simple Example
Here's a simple program that uses the dynamic keyboard method to do something normally forbidden by BASIC: a computed GOTO. In most cases, a straightforward ONGOTO command does the same job better, but let's use this example for the sake of simplicity. Type in line 100 as shown for your machine:

For VIC-20 or Commodore 64:

100 DATA 198,631

For Plus/4 or 16:

100 DATA 239,1319

For most PET/CBM:

100 DATA 158,623

Now enter the following lines:

110 READ A,B
140 IF L<3 OR L>5 THEN 130
150 PRINT CHR$(147)
180 PRINT "GOTO";L*100
190 PRINT CHR$(19)

    The program isn't finished, but you might like to see what we have so far. If you run it and enter 3 in response to the prompt, you'll find the program stopped with the cursor blinking over a line that says GOTO 300. To execute that direct command, all you'd need to do is press RETURN. When we complete the program, it will press RETURN by itself. Finish the program by entering these lines:

200 POKE A,1
210 POKE B,13
220 END
310 GOTO 120
400 PRINT "HERE'S 400"
410 GOTO 120

    It's as easy as that. Once you grasp the basic method, all sorts of interesting applications come to mind. Next time, we'll look at more advanced, useful applications of the dynamic keyboard technique.