Classic Computer Magazine Archive COMPUTE! ISSUE 144 / SEPTEMBER 1992 / PAGE G18

What does this key do? (programming function keys on the computer) (Beginner BASIC)
by Larry Cotton

Back in March 1991, I asked you readers for some help in writing this column. I got lots of suggestions, for which I thank you! This column is in response to one reader who asked that I explain the keys unique to the 64 and 128.

The 64 and 128 are blessed with special keys that other computers don't have. (The 128 also has 26 extra keys, which give it even more functionality.) Some of the 66 keys common to the 64 and 12 have dedicated functions, some seem to work some of the time, and others seem to do absolutely nothing.

As a general statement, all keys are internally hard-wired to do particular things. By clever BASIC programming, however, you can override most of their normal functions until the power is turned off. For instance, in SpeedScript, which I'm using right now, the left-arrow key is programmed to move the cursor to the left, deleting as it moves. Epyx's Fast Load cartridge, however, programs that same key to save a BASIC program.

Those two examples happen to be programmed in machine language, but we can do the same things (albeit more slowly) in BASIC. To do this, we should know two things about each key: its CHR$ code and its keyboard matrix value. The CHR$ codes are contained in the computer's manual and other reference guides. The other values can be determined by entering this short program. 10 PRINTPEEK(197),PEEK(653) 20 GOTO10 When you run it, you'll see two values that represent what's in these memory registers, depending on which keys are pressed. When no key is pressed, 197 contains 64, and 653 contains 0.

Each key (except Shift, Ctrl, Commodore, and Restore) causes a unique value to appear in 197, based on the keyboard's wiring. If two or more keys are pressed simultaneously, one of the keys will dominate the others. Note that even the Run/Stop key has a value (63), which you can see by holding Shift and pressing the Run/Stop key.

The value in 653 depends on the status of the Shift, Ctrl, and Commodore (C=) keys; the values are additive, as shown below. Key Pressed 653's Value Shift 1 C= 2 Shift/C= 3 Ctrl 4 Shift/Ctrl 5 Ctrl/C= 6 Shift/Ctrl/C= 7

Let's write another short BASIC SIC program that illustrates how to put this knowledge to work for us by programming the function keys to change background and text colors. 10 PRINTCHR$(147) 20 K=PEEK(197): S=PEEK(653) 30 IFK<30RK>6THEN20 40 IFS=0 THEN ON K-2

GOTO100,110,120,130 50 IFS=1 THEN ON K-2

GOTO140,150,160,170 100 C=5: W=0: GOSUB500:

GOTO20 110 C=6: W=1: GOSUB500:

GOTO20 120 C=3: W=2: GOSUB500:

GOTO20 130 C=4: W=3: GOSUB500:

GOTO20 140 C=7: W=4: GOSUB500:

GOTO20 150 C=10: W=6: GOSUB500:

GOTO20 160 C=11:W=5: GOSUB500:

GOTO20 170 C=12:W=7: GOSUB500:

GOTO20 500 POKE53280,C: POKE53281,C:

POKE646,W 510 PRINT "LIKE THIS COMBINATION?

[UP]": RETURN Line 20 checks memory registers 197 and 653. Since we're looking only for function key activity, we weed out all other keyboard matrix values in line 30. Lines 40 and 50 determine whether the Shift key is pressed or not and then the ON-GOTO statement is used. 10 PRINTCHR$(147):POKE646,1 20 BG=53281:BO=53280 30 GETA$:IFA$=" "THEN30 40 V=ASC(A$): IFV<133 ORV>140

THEN30 50 ONV-132 GOTO100, 110, 120,

130, 140, 150, 160, 170 60 GOTO30 100 BG=5:BO=0: GOSUB500:

GOTO30 110 BG=6:BO=1: GOSUB500:

GOTO30 120 BG=3:BO=2: GOSUB500:

GOTO30 130 BG=4:BO=3: GOSUB500:

GOTO30 140 BG=4:BO=8: GOSUB500:

GOTO30 150 BG=10:BO=6: GOSUB500:

GOTO30 160 BG=11:BO=5: GOSUB500:

GOTO30 170 BG=12:BO=7: GOSUB500:

GOTO30 500 POKE53281,BG:

POKE53280,BO: PRINT

"LIKE THIS COMBINATION?

[UP]":RETURN Line 30 waits for a keypress. Line 40 gets the ASCII value of A$, subtracts 132, and then uses ON-GOTO to choose the appropriate line.

Of course, you'll probably want to do more important things than just change colors with the function keys, but this should give you enough information to get started. Next month we'll look at more unique keys.