Classic Computer Magazine Archive COMPUTE! ISSUE 70 / MARCH 1986 / PAGE 99

Mousify Your Applesoft Programs

Lee Swoboda

Here's the first installment of a two-part tutorial on interfacing and using a mouse with your Apple II computer. Though a mouse is preferred, you can use the techniques shown here to substitute a joystick or game paddles for the mouse.

Allow me to introduce a new word—mousify. Don't try to look that up in a dictionary—it's not there, at least not yet. Though Apple didn't invent the mouse, their Macintosh was the first popular home computer that used one; and it has changed the way that we interface with computers. So the need for the word mousify—to add mouse control to computer hardware or software—may grow.

A Mouse For Apple II

Apple II computers don't come with a mouse, but it's now possible to add one. If you have an Apple II, 11+, or He, you'll need an interface card (Apple product A2M2050, $149.95 including the mouse). The interface is built into the lie, so you only need to buy the mouse device (Apple product A2M4015, $99.95).

Attaching a mouse to your Apple II is only half the battle. To your computer, a mouse is merely another input device, like a joystick. Many new programs have the ability to accept mouse input. But there are thousands of existing programs that were written before the mouse appeared on the scene. Most commercial programs are written in machine language and protected from unauthorized access so, unless you are an expert machine language programmer, you won't be able to mousify them. However, Applesoft BASIC programs can easily be mousified.

Pages 35–40 of the AppleMouse II User's Manual, which comes with the mouse, give a brief description of how to use the mouse in a BASIC program. But the two examples are trivial and the text fails to mention some of the "features" that make mousifying an Applesoft program difficult. For example, you must use the INPUT statement to obtain mouse position parameters. Unfortunately, the INPUT command erases one line of screen text. In addition, your program must set the computer to receive input, which disconnects the keyboard. Mixing mouse and keyboard input requires switching input control between the mouse and the keyboard. Not only that, you have to use GET statements for keyboard input, which can be exasperating. Using these techniques quickly turns your BASIC program into a Rube Goldberg contraption of INPUTs, GETs and PRINT D$'s. But there's a better way to handle the mouse.

Fortunately, Apple's input and output (I/O) are memory-mapped, meaning that the keyboard and each character on the 40-column screen are at specific locations in Apple's main memory. Applesoft's PEEK and POKE commands let us examine and change characters on the screen without having to use INPUTs, GETs or PRINTs. This is especially important when using the mouse, since it lets you zoom quickly to any point on the screen to change a character, rather than perform complex string manipulations.

Practical Application

Let's see how this works. Type in and save Programs 1 and 2, then load and run Program 2, which creates a text file for Program 1 to read. When that's done, load and run Program 1. You'll see an input screen, typical of what might be used in an address book program that lets you store and recall names and addresses. (Of course, this program is just for demonstration; you can't use it to create a real address file.)

In a typical program, the computer would make you reenter an entire line to correct a misspelling or other error. Program 1 lets you point directly at an error with the mouse, and change only the incorrect character. The initial screen display should contain this information:

ENTER INFORMATION

FIRST NAME COMPUTE!
LAST NAME REEDER SERVICE
STREET P.O. BOX 2141
CITY RANDOR
STATE PA
ZIP 19089
TELEPHONE 1-800-334-0868

ERASE QUIT DONE HELP

Notice that the word READER is misspelled REEDER. That's the mistake you'll correct. Also notice there are three flashing rectangles on the screen. The rapidly flickering rectangle in the upper-right corner is produced when Program 1 obtains mouse input (lines 10150–10160). This effect is always present, but is unimportant to the present discussion. The flashing C at the beginning of the word COMPUTE! is the cursor. The blinking reflex (ˆ) in the upper left corner of the screen is the mouse pointer. The mouse moves the mouse pointer around the screen.

Move the mouse so the mouse pointer replaces the second E in REEDER and press the mouse button. The computer immediately moves the cursor to the same spot. Now type the letter A (upper or lower case) to correct the spelling mistake. That's all you need to do to correct the error. You can also use the arrow keys to move the cursor (Apple II uses the CTRL-J and CTRL-K keys to simulate the up and down arrow keys of the IIe and IIc). But the mouse moves the cursor much more rapidly, and is far more intuitive to use.

Now move the pointer to the word DONE in the strip menu at the bottom of the screen, and press the button. The computer reads the information from screen memory and, in this case, redisplays the updated information. Of course, in a working program you would replace lines 30120–30190 with routines that store the data for later recall. You can move the mouse pointer or cursor anywhere on the screen, but line 10710 of the program prevents you from typing anything outside the text area.

How The Program Works

Let's take a closer look at the significant portions of Program 1. Lines 130 and 10000–10830 are the most important. Line 130 sets the sensitivity of the mouse. When MI has a value of 20, the pointer moves to any part of the 40-column screen as the mouse moves within a 5 × 8 inch area. Give MI a larger value to make the mouse less responsive.

Lines 10070–10090 activate the mouse. Input control is transferred from the keyboard to the mouse, until line 20030 returns control to the keyboard. Lines 10150–10270 calculate the horizontal and vertical position of the mouse and determine whether the mouse button has been pushed. Line 10170 and lines 10440–10760 handle input from the keyboard. Note that input control remains with the mouse at this point: The program does not use the statement PRINT D$"IN#0" to return control to the keyboard. This is the key to the simplicity of Program 1, since it avoids the problems normally encountered when using GET and PRINT commands with DOS.

Line 10220 and lines 10230–10270 move the cursor to the same position as the mouse pointer when you press the mouse button. Lines 10320–10390 position the mouse pointer and return to line 10150 to read the mouse again. If you don't press a key or the mouse button, the computer stays in the loop from 10150 to 10390, reading the mouse and repositioning the mouse pointer. Lines 10590–10620 position the cursor. This routine is activated only when you press an arrow key or the mouse button. Lines 10640–10690 change all upper-and lower-case and all inverse characters to flashing.

Substituting A Joystick Or Game Paddles

If you don't have a mouse, you can use a joystick (or, less conveniently, game paddles) to achieve the same effects. With a few modifications, Program 1 can be made to accept joystick or paddle input. Here are the steps to follow:

  1. Delete lines 120, 130, 10001–10090 and 20220.
  2. Modify the following lines as shown:
    CB 10150 X0=PDL(0)
    S4 10160 Y0=PDL(1)
    AS 10170 IFB0>127THEN10440
    A9 10180 Y0=INT(Y0/10)+1
    47 10200 X0=INT(X0/6)+1
    60 10220 IFB0<12BTHEN10320
    32 20030 REM
  3. Add the following lines:
    6D 10165 B0=PEEK(-16384)
    54 10215 B0=PEEK(-16287)

After making these changes, resave Program 1, using a different filename to distinguish it from the original version. When you run it, the joystick moves the mouse pointer around the screen and the button works just like the mouse button. At this point you might wonder why anyone would buy a mouse, since a joystick or game paddle seems to work as a substitute. Part of the reason is simply preference—many people find that a real mouse "feels" better and is therefore more convenient than a joystick. More significantly, most commercial programs that accept mouse input do not recognize input from a joystick or paddles. If you're writing programs strictly for your own use, a joystick may serve the purpose; but if you buy commercial software that requires a mouse, you may have no choice.

Using a mouse is a new experience for many Apple II owners. I hope this program inspires you to mousify some of your own programs. In Part 2 of this article we'll expand the capabilities of Program 1 to let you use the mouse to delete and insert blocks of text.

Program 1. Apple II Mouse Demonstration

For instructions on entering this listing, please refer to "COMPUTEI's Guide to Typing In Programs" published in this issue of COMPUTE!.

BD 120 S0 = 2: REM SLOT CONTAINING MOUSE
D7 130 MI = 20: REM MOUSE SENSITIVITY
5A 140 D$ = CHR$ (4)
8C 150 REM
07 160 REM READ DATA FILE
90 170 REM
CB 180 PRINT D$"OPEN TEXT"
32 190 PRINT D$"READ TEXT"
6D 200 INPUT NF$, NL$, AD$, CI$, ST$, ZI$, TE$
CD 210 PRINT D$"CLOSE TEXT"
87 220 REM
25 230 REM DATA ENTRY SCREEN
8B 240 REM
4F 250 HOME
4D 260 Y1 = 4:X1 = 15:C0 = 160
35 270 INVERSE
D7 280 PRINT "     ENTERINFORMATION       "
2C 290 VTAB 24: PRINT "MENU: ERASE QUIT DONE HELP
            ";
C6 300 NORMAL
31 310 VTAB 4; HTAB 1
F4 320 PRINT "FIRST NAME.."
C6 330 PRINT "LAST NAME..."
3C 340 PRINT "STREET......"
D6 350 PRINT "CITY........"
1F 360 PRINT "STATE ......"
36 370 PRINT "ZIP........."
17 380 PRINT "TELEPHONE..."
3A 390 VTAB 19: HTAB 10: INVERSE : PRINT "ˆ": NORMAL
81 400 PRINT " IS MOUSE POINTER"
3C 410 VTAB 21: HTAB 14: INVERSE : PRINT " ": NORMAL
38 420 PRINT " IS CURSOR"
26 430 VTAB 4
5E 440 HTAB 15: PRINT NF$