Classic Computer Magazine Archive COMPUTE! ISSUE 60 / MAY 1985 / PAGE 86

Dynamic Function Keys For VIC & 64

Albert Chu

Now you can put those do-nothing special function keys on your computer to work. The accompanying programs provide several handy functions for BASIC programmers—available at the touch of a key.

Not too long ago, special programming aids that added new keyboard features to your computer had to preempt certain seldom-used keys, or else require you to press an awkward combination of keys. A conflict arose when you wanted to use the key for its original purpose. What then?

Fortunately, most of today's computers come with all-purpose programmable function keys. On some computers (such as the Commodore Plus/4, 16, and IBM PC/PCjr), these special function keys are preprogrammed to perform various tasks. But on other computers (such as the Commodore 64 and VIC-20), the function keys do nothing. It's up to you to program them.

The f1-f8 keys on the 64 and VIC are quite easy to program in BASIC. It's simply a matter of detecting the keypress with an IF-THEN statement and passing control to the appropriate subroutine. However, if the functions you want to assign to the keys are programming aids, the BASIC program that reads the keys will interfere with the BASIC program you're working on. What's needed is a machine language program that runs transparently, in the "background."

That's exactly what you get with "Dynamic Function Keys." It reprograms all eight functions of the four keys on the 64 and VIC. With a single keypress, you can instantly change the screen colors, freeze a listing, clear a portion of the screen, and much more. And the machine language program runs invisibly in the background, leaving your BASIC program intact.

Eight Programming Aids

Here's a list of features provided by Dynamic Function Keys:

f1 changes the screen colors for a more readable display—cyan characters on a black background.

f2 changes the screen colors to black on white for the VIC, and black on light gray for the 64. This combination is especially readable when using the Supermon machine language monitor.

f3 enables automatic-repeat for all the keys, just like the cursor keys. Simultaneously press f3 and the key you want to repeat, then release f3 while holding down the other key. (On the 64, you don't have to release f3 to keep the key repeating.)

f4 exits the infamous Commodore "quote mode." When editing the text between quotes in a PRINT statement, the cursor can stop acting normally—cursor movements are stored within the string as reverse characters, instead of being interpreted literally. Usually you must hit RETURN or SHIFT-RETURN to escape from quote mode, then move the cursor back up to the line and begin editing again. Now you can simply press f4 to escape from quote mode.

f5 is a general-purpose pause key. It temporarily freezes almost all visible activities of the computer. By pressing f5, you can temporarily stop a listing from scrolling, then start it again. (Sometimes this causes a line to be listed twice on the screen; this can be safely ignored.) Or you can halt a running BASIC program. To unfreeze the computer, press the Commodore key at the bottom-left corner of the keyboard.

f6 tabs the cursor to a predetermined tab stop. On the 64, tabs are ten spaces apart; on the VIC, f6 tabs the cursor to the right screen margin.

f7 clears the screen from the top line to the cursor position.

f8 clears the screen from the cursor position to the bottom line.

Hints For Use

Be sure to type in the right listing for your computer; the programs are very similar but not identical. If you have a VIC with 8K or more memory expansion, substitute the modifications in Program 3 for lines 220 and 300 in Program 2.

As always, save the program on disk or tape before running it for the first time. The program is a BASIC loader which POKEs the machine language into memory. After it runs, type NEW to erase the BASIC portion. Then type SYS 750 to activate the machine language portion.

Before running a BASIC program of your own—especially a program that defines the function keys for its own purposes—you should disable Dynamic Function Keys by pressing RUN/STOP-RESTORE. Otherwise, the programs may interfere with each other, or you could blank out a section of the screen by accidentally hitting f7 or f8. Of course, if you want to freeze program execution with f5, you'll have to keep Dynamic Function Keys active.

To reenable Dynamic Function Keys, enter SYS 750.

I recommend that you disable the utility before executing any input/output operations, such as saving or loading a program.

Programmer's Notes

Dynamic Function Keys is an interrupt-driven routine that resides at the top of memory. Although you activate it with SYS 750, that address contains only an instruction to jump to the section of code that links the program to the IRQ. Dynamic Function Keys works by altering an interrupt vector which points to routines in Read Only Memory (ROM) for checking the RUN/STOP key, updating the clock, and performing other chores. The program alters this vector to point to its own routines instead.

The values in the A, X, and Y registers are stored on the 6502/6510 stack whenever the computer calls the new interrupt routine every 1/60 second. It is essential to save the contents of these registers so that everything can be restored when returning from the interrupt.

During each interrupt, the program checks to see if a function key has been pressed. If none was, the program restores the registers and continues with the normal IRQ routine.

If a key was pressed, the program checks to see if the screen editor is in quote mode. If so, it continues with the normal interrupt, unless f4 (the quote mode exit key) was pressed. This lets you imbed function key codes in a string as usual without the new functions getting in the way. If you want to use f4 in a BASIC program, refer to it with CHR$(138).

After it detects that a function key was pressed, the program checks the SHIFT key (to distinguish between f1 and f2, for example). Then it executes the appropriate function.

Interpreting The Keys

If you pressed f1 or f2 (to change screen colors), Dynamic Function Keys simply places the color codes into the screen color registers.

When f3 is pressed, the program sets the key repeat flag at memory location 650 (decimal) to 128 until the key is released. Then it stores a 0 to turn off the automatic repeat.

The f4 (exit quote mode) key triggers the program to test for quote mode. The escape option is bypassed if the screen editor is not in quote mode. Otherwise, the program stores zeroes in three memory locations: 212 (the quote mode flag), 216 (the number of inserts left), and 199 (the reverse-character flag). Next, it erases the characters left by the f4 key and moves the cursor backward one space to cover up any signs the f4 key left behind.

The f5 (pause) key does not actually stop the computer, of course. Instead, the computer repeatedly scans the keyboard until the Commodore key is pressed. Only then will the machine exit the loop and continue with the interrupt. Since the keyboard buffer remains active during this looping, any other keys you press while the computer is frozen will be executed after it "thaws." If you press f5 when no program is running, the cursor will disappear until you hit the Commodore key.

When you push f6 (tab), the program determines the cursor position by calling a Kernal routine, and then moves the cursor by calling another Kernal routine.

To clear a portion of the screen when you press f7 or f8, the program first finds the cursor position. Locations 209 and 210 contain the low and high bytes of the memory address corresponding to the cursor's row, and location 211 indicates the cursor position in that row. Then the program fills screen memory with spaces either above or below the cursor position.