Fourmilab Home Page

Key Clicker

Commodore 64

My first Commodore program, “Key Clicker”, was published in the April 1988 issue. Here's how it appeared in the magazine.

Article as Published

Compute's Gazette, April 1988, cover

Compute's Gazette, April 1988, page 63

Compute's Gazette, April 1988, page 63

Original Article as Submitted

When you submit something to a mass-market magazine, expect it to be edited, if not almost rewritten for style and space considerations. Here is the article and programs as originally submitted. Commodore BASIC programs were written in all upper case letters, but I've presented them here in lower case as it's easier for most people to read.

Most people, especially those who started with computers back in the age of mechanical teletypes, appreciate the golden silence of a modern computer's keyboard. Every now and then, however, while demonstrating your computer to a friend, you'll get the question, “Gee, it doesn't sound like a computer, does it? Isn't it supposed to beep or something?”. More practically, if you're entering large volumes of data, especially tables of numbers, it's nice to have confirmation that you hit a key without having to look up at the screen.

Key Clicker is a program that makes your computer sound like a computer. Or, for that matter (since the sounds it makes are limited only by your creativity in programming the Commodore's sound synthesizer) anything else you'd like it to, for practical applications and practical jokes, from the sublimely serious to the supremely silly.

Getting Started

Key Clicker comes in two versions, one for the Commodore 64 (or 128 in 64 mode), and another for the 128 in 128 mode. Be sure to use the correct version for your computer and the mode you're running in. To use Key Clicker, first type in the correct version of the program and save a copy on tape or disk. To activate the program, just load it and type RUN. The program clears the screen and asks you:

SELECT SOUND:
  COMPUTER
  TYPEWRITER
  DRAMATIC
SELECTION?

and then waits for your reply (you need only enter the first letter of the desired sound). “Computer” makes a little “boop” each time you press a key, just like movie computers. “Typewriter” sounds like an old manual typewriter or teletype machine. “Dramatic” is for heavy programming; it's the same as “typewriter” but the RETURN key makes a sound commensurate with the gravity of your work.

Getting On With It

After you answer the prompt, Key Clicker installs itself and from that point on (until you reset the computer or load a conflicting program), keys will make the sound you've selected. To choose another kind of sound, just run Key Clicker again. Once you've installed Key Clicker, you can load and execute other programs; Key Clicker continues to sound off. Key Clicker copies a small piece of machine language into a normally-unused area of memory ($2A7$2CE on the 64, $B00$B2A on the 128), and splices this code into the keyboard processing loop. Key Clicker will continue to work until the machine is reset, or until some other program reprograms the SID (sound synthesizer) chip or overwrites the area where the machine language is stored. Note that the 128 version stores the machine language in the tape buffer, so cassette operations will destroy it.

Getting Inside It

If you want to make Key Clicker generate different sounds, you can change the values loaded into the SID chip. These appear as DATA statements in lines 460–490 of both versions of the program. The first three lines are the parameters for voices 1 through 3 of the SID chip, and line 490 specifies the control and filter values. Voice 1 is used when “Computer” mode is selected. Voice 2 generates all sound for “Typewriter” mode, and the sound for everything except RETURN in “Dramatic” mode. Voice 3 generates the “Dramatic” mode RETURN key sound. For details on programming the SID chip, refer to a reference book such as Programming the Commodore 64 by Raeto Collin West from COMPUTE! Books (the SID and its programming are identical in the 64 and 128). A little SID wizardry and you can amaze your friends with a computer that chugs like the little engine that could, quacks like a duck, or thunders like waves breaking on the beach, all in time with your typing.

The machine language part of the program differs in the 64 and 128 versions because the ROM services the keyboard differently in the two machines. You can move the machine language to another free memory area just by changing the assignment to variable A in line 140—the code is fully relocatable (on the 128 you must locate the machine language in an area of memory accessible while the ROM has control). If you wish to modify the machine language part, note that lines 330–400 patch the machine language after it has been loaded; be sure to change them accordingly.

Original Program Listings

C64 Version

10 rem key clicker - c64 version
20 rem initialise the sid chip
30 sid = 54272
40 for i = sid to sid+24
50 poke i,0
60 next i
70 rem load sounds into sid chip
80 for i = sid to sid+24
90 read k
100 poke i,k
110 next i
120 rem load machine language part
130 rem a = start address (relocatable)
140 a = 679 : rem $2a7
150 for i=0 to 39
160 read k
170 poke a+i,k
180 next i
190 rem choose sound and patch voice
200 rem selection in ml routine
210 print "";
220 print "select sound: "
230 print "  computer"
240 print "  typewriter"
250 print "  dramatic"
260 input "selection"; s$
270 s$=left$(s$,1)
280 if s$="c" then s1=4:s2=4:b=64:goto 330
290 if s$="t" then s1=11:s2=11:b=128:goto330
300 if s$="d" then s1=11:s2=18:b=128:goto330
310 print "unknown sound, try again."
320 goto 220
330 poke a+20,s1
340 poke a+25,s1
350 poke a+31,s2
360 poke a+36,s2
370 poke a+18,b
380 poke a+29,b
390 poke a+23,b+1
400 poke a+34,b+1
410 rem splice into keyboard vector
420 ah = int(a / 256)
430 al = a - 256 * ah
440 poke 655,al : poke 656,ah
450 rem sid register contents
460 data 0,28,0,4,64,19,0
470 data 0,255,0,0,128,19,0
480 data 0,32,0,12,128,11,0
490 data 0,50,245,47
500 rem machine language routine
510 data 165,198,72,32,72,235,104,197
520 data 198,240,16,165,197,201,1,240
530 data 11,169,128,141,11,212,169,129
540 data 141,11,212,96,169,128,141,18
550 data 212,169,129,141,18,212,96,64

C128 Version

10 rem key clicker - c128 version
20 rem initialise the sid chip
30 sid = 54272
40 for i = sid to sid+24
50 poke i,0
60 next i
70 rem load sounds into sid chip
80 for i = sid to sid+24
90 read k
100 poke i,k
110 next i
120 rem load machine language part
130 rem a = start address (relocatable)
140 a = 2816 : rem cassette buffer
150 for i=0 to 42
160 read k
170 poke a+i,k
180 next i
190 rem choose sound and patch voice
200 rem selection in ml routine
210 print "";
220 print "select sound: "
230 print "  computer"
240 print "  typewriter"
250 print "  dramatic"
260 input "selection"; s$
270 s$=left$(s$,1)
280 if s$="c" then s1=4:s2=4:b=64:goto 330
290 if s$="t" then s1=11:s2=11:b=128:goto330
300 if s$="d" then s1=11:s2=18:b=128:goto330
310 print "unknown sound, try again."
320 goto 220
330 poke a+22,s1
340 poke a+27,s1
350 poke a+33,s2
360 poke a+38,s2
370 poke a+20,b
380 poke a+31,b
390 poke a+25,b+1
400 poke a+36,b+1
410 rem splice into keyboard vector
420 ah = int(a / 256)
430 al = a - 256 * ah
440 poke 828,al : poke 829,ah
450 rem sid register contents
460 data 0,28,0,4,64,19,0
470 data 0,255,0,0,128,19,0
480 data 0,32,0,12,128,11,0
490 data 0,50,245,47
500 rem machine language routine
510 data 201,13,8,170,165,208,72,138
520 data 32,173,198,104,197,208,240,25
530 data 40,240,11,169,128,141,11,212
540 data 169,129,141,11,212,96,169,128
550 data 141,18,212,169,129,141,18,212
560 data 96,40,96