Classic Computer Magazine Archive COMPUTE! ISSUE 42 / NOVEMBER 1983 / PAGE 271

Using The VIC/64 Function Keys

Jim Butterfield, Associate Editor

The function keys, fl to f8, seem easy to use and understand. Yet, if you haven't made the right mental connections, they must seem baffling. One of the questions we're asked most often is "How do you make the functions keys work?"

You Can't Input

Let's talk about the INPUT statement for a moment. If your program contains an INPUT statement – or for that matter, if you try typing a direct command – the function keys don't seem to work. They really do work, but to little avail.

The point here is that INPUT takes its information from the screen – and the function keys don't show up on the screen.

When you press one of the keys, it is received and placed into the keyboard input buffer. During an input or direct statement – in other words, whenever the cursor is flashing – the keyboard buffer is promptly emptied and the characters there are printed to the screen. There's the problem: fl, f2, etc., have no printable equivalent, so at this point the characters are lost. Later, INPUT will see you press RETURN and will take its information from the screen, but there are no f-characters there.

In other words, INPUT or normal direct statements will lose the function keys. There's a way around this, but it's awkward: put your input within quotation marks, and the keys will be detected. They will also print oddly, but that's another story.

Another Story – GET

The GET command takes information directly from the keyboard buffer, so it will read these keys without problem. The question is: how does your program test to see if it has an f-key? The answer is easy, but it's rather graphic in nature – so try this on your machine:

100 GET X$
110 IFX$ = "

and hold it right there.

At this moment, we're in the middle of line 110, and we've just typed the quotation mark. Now, press the fl key, and you'll see an odd reverse graphic symbol printed. It's the "programmed cursor" equivalent of the key fl; it looks like a reversed horizontal bar, and for all intents and purposes it is key f1. Now finish the line so it looks like this:

110 IF X$ = "f1" THEN PRINT "FUNCTION 1"

Note to readers who have been skimming: don't type the characters f and 1 within the quotation marks; tap key fl at this point.

Using the same system, we may work through all eight functions:

120 IF X$ = "f 2" THEN PRINT "FUNCTION 2"

Key f2 is fl with the shift key held down, of course; it prints a reverse quarter-circle. Keep going:

130 IF X$ = "f3" THEN PRINT "FUNCTION 3"

and so on until:

180 IF X$ = "f8" THEN PRINT "FUNCTION 8"
190 GOTO 100

You can run this program and play with the f-keys as long as you like. As you can see, the computer recognizes the keys without trouble.

In larger programs, you'll often want to GOTO when you see a given key. That's no trouble at all, of course.

Another Way

Those funny characters can be puzzling. They are hard to read and maybe confused with each other. There's no listing standard for them yet. Sometimes it's useful to write them another way, without the funny characters.

100 GET X$ : IF X$ = " " GOTO 100
110X = ASC(X$)

We've changed our input key to an ASCII number. Every key has its own ASCII value; if we know the value, we'll know which key.

Now I could tell you the ASCII values for the eight function keys, but I'm not going to do that. Instead, I'll tell you how to find these values for yourself.

Suppose you want to find the ASCII number for key fl. Just' type:

PRINT ASC("fl")

Remember to press the fl key (don't type f and 1 as two characters), and you'll see the computer respond with a value of 133. That's the ASCII value of key f1.

Now we can continue the above program:

120 IF X = 133 THEN PRINT "FN 1"
130 IF X = 137 THEN PRINT "FN 2"

continuing to:

190 IF X = 140 THEN PRINT "FN 8"
200 GOTO 100

I haven't given you the ASCII numbers to fill in the missing lines – but with a little care and attention, you can find them for yourself.

A Simple Example

Let's do a simple quiz, using the odd-numbered f-keys.

100 DATA WHO DISCOVERED AMERICA
110 DATA GALILEO, COLUMBUS, REAGAN, EINSTEIN
120 DATA 2
130 DATA THE CHARGE ON AN ELECTRON IS
140 DATA NEUTRAL,POSITIVE,NEGATIVE,VARIABLE
150 DATA 3
160 DATA UGANDA IS IN
170 DATA ASIA, SOUTH AMERICA, EUROPE, AFRICA
180 DATA 4
190 DATA "*"
200 READ Q$ : IF Q$ = "*" THEN END
210 PRINT Q$; "--"
220 READ A$ : PRINT"F1 – " ;A$
230 READ A$ : PRINT"F3 – " ;A$
240 READ A$ : PRINT"F5 – " ;A$
250 READ A$ : PRINT"F7 – " ;A$
260 PRINT "YOUR ANSWER? ";
270 READ A
280 GET X$ : IF X$ = "" GOTO 280
290 X = ASC(X$)
300 IF X < 133 OR X > 136 GOTO 280
310 X = X - 132 : PRINT "F" ;X * 2 - 1
320 IF X = A THEN PRINT "RIGHT!" : GOTO 340
330 PRINT "WRONG!"
340 GOTO 200

You'll notice that line 220 calls for you to type the actual characters (F and 1), and the same goes for lines 230 to 250.

The program isn't the definitive educational package – but it does show how the function keys can be used effectively.

Without A Program

Sometimes you might like to have the function keys do something even when there is no program running. That's much tougher: if your program is not running, it can't do the job. You may have noticed that packages like the Super Expander provide this feature: pressing the fl key might produce the word GRAPHIC on the screen.

This kind of thing utilizes advanced techniques. You would need to know machine language, and how to implement a wedge. It can be done – but it's not for beginners.