Jan 98 - Getting Started
Volume Number: 14
Issue Number: 1
Column Tag: Getting Started
Event-Based Programming
by Dave Mark
How a Mac program communicates with the user
Over the last year or so, we've gotten a lot of feedback about the direction in which you
want this column to head. Some folks want more coverage of Java, others PowerPlant
and Rhapsody. But the biggest vote of all was a return, for a spell, to the basics. When I
first started this column more than six years ago (For you old-timers, my son Daniel,
who was born in these pages, is now 5-1/2), we plowed a path for beginners,
covering the basics of working with the Mac Toolbox. Since then, we've explored a wide
variety of topics and have covered a lot of ground.
Over the next few months, we're going to revisit some of that territory at the behest of
a new generation of Mac programmer. We started with last month's column, which
gave an updated answer to the question, "How do I get started with Mac programming?
This month, we'll revisit the concept of event-based programming.
Know someone interested in getting started with Mac programming? Hand them your
copy of last month's MacTech and point them this way...
Event-Based Programming
Most the programs we've created together have one thing in common. Each performs its
main function, then sits there waiting for a mouse click using this piece of code:
while ( ! Button() )
;
This chunk of code represents the only mechanism the user has to communicate with
the program. In other words, the only way a user can talk to one of our programs is to
click the mouse to make the program disappear! This month's program is going to
change all that.
One of the most important parts of the Macintosh Toolbox is the Event Manager. The
Event Manager tracks all user actions, translating these actions into a form that's
perfect for your program. Each action is packaged into an event record and each event
record is placed on the end of the application's event queue.
For example, when the user presses the mouse button, a mouseDown event record is
created. The record describes the mouseDown in detail, including such information as
the location, in screen coordinates, of the mouse when the click occurred, and the time
of the event, in ticks (60ths of a second) since system startup. When the user releases
the mouse button, a second event, called a mouseUp event is queued.
If the user presses a key, a keyDown event is queued, providing all kinds of
information describing the key that was pressed. An autoKey event is queued when a