Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Macintosh Toolbox Essentials /
Chapter 2 - Event Manager


Summary of the Event Manager

Pascal Summary

Constants

CONST {event codes}
      nullEvent               = 0;        {no other pending events}
      mouseDown               = 1;        {mouse button pressed}
      mouseUp                 = 2;        {mouse button released}
      keyDown                 = 3;        {key pressed}
      keyUp                   = 4;        {key released}
      autoKey                 = 5;        {key repeatedly held down}
      updateEvt               = 6;        {window needs updating}
      diskEvt                 = 7;        {disk inserted}
      activateEvt             = 8;        {activate/deactivate window}
      osEvt                   = 15;       {operating-system events }
                                          { (suspend, resume, mouse-moved)}
      kHighLevelEvent         = 23;       {high-level events }
                                          { (includes Apple events)}
      
      {event masks}
      everyEvent              = -1;       {every event}
      mDownMask               =  2;       {mouse-down event       (bit 1)}
      mUpMask                 =  4;       {mouse-up event         (bit 2)}
      keyDownMask             =  8;       {key-down event         (bit 3)}
      keyUpMask               =  16;      {key-up event           (bit 4)}
      autoKeyMask             =  32;      {auto-key event         (bit 5)}
      updateMask              =  64;      {update event           (bit 6)}
      diskMask                =  128;     {disk-inserted event    (bit 7)}
      activMask               =  256;     {activate event         (bit 8)}
      highLevelEventMask      =  1024;    {high-level event       (bit 10)}
      osMask                  = -32768;   {operating-system event (bit 15)}

      {message codes for operating-system events}
      suspendResumeMessage    = $01;      {suspend or resume event}
      mouseMovedMessage       = $FA;      {mouse-moved event}
      osEvtMessageMask        = $FF000000;{can use to extract msg code}
      {flags for suspend and resume events}
      resumeFlag              = 1;           {resume event}
      convertClipboardFlag    = 2;           {Clipboard conversion }
                                             { required}
      {event message masks for keyboard events}
      charCodeMask= $000000FF;               {use to get character code}
      keyCodeMask = $0000FF00;               {use to get key code}
      adbAddrMask = $00FF0000;               {ADB address for ADB keyboard}

      {constants corresponding to bits in the modifiers field of event}
      activeFlag  = 1;        {bit 0 of low byte--valid only for }
                              { activate and mouse-moved events}
      btnState    = 128;      {bit 7 of low byte is mouse button state}
      cmdKey      = 256;      {bit 0 of high byte}
      shiftKey    = 512;      {bit 1 of high byte}
      alphaLock   = 1024;     {bit 2 of high byte}
      optionKey   = 2048;     {bit 3 of high byte}
      controlKey  = 4096;     {bit 4 of high byte}

      {high-level event posting options}
      nAttnMsg                = $00000001;   {give this message priority}
      priorityMask            = $000000FF;   {mask for priority options}
      nReturnReceipt          = $00000200;   {return receipt requested}
      systemOptionsMask       = $00000F00;   
      receiverIDisTargetID    = $00005000;   {ID is port name & location}
      receiverIDisSessionID   = $00006000;   {ID is PPC session ref number}
      receiverIDisSignature   = $00007000;   {ID is creator signature}
      receiverIDisPSN         = $00008000;   {ID is process serial number}
      receiverIDMask          = $0000F000;
      
      {class and ID values for return receipt}
      HighLevelEventMsgClass  = 'jaym';      {event class of return receipt}
      rtrnReceiptMsgID        = 'rtrn';      {event ID of return receipt}
      
      {modifiers values in return receipt}
      msgWasNotAccepted       = 0;           {recipient did not accept }
                                             { the message}
      msgWasFullyAccepted     = 1;           {recipient accepted the}
                                             { entire message}
      msgWasPartiallyAccepted = 2;           {recipient did not accept }
                                             { the entire message}

Data Types

TYPE
   EventRecord = 
   RECORD
      what:       Integer;       {event code}
      message:    LongInt;       {event message}
      when:       LongInt;       {ticks since startup}
      where:      Point;         {mouse location}
      modifiers:  Integer;       {modifier flags}
   END;

   KeyMap = PACKED ARRAY[0..127] OF Boolean; {records state of keyboard}

   TargetID = 
   RECORD
      sessionID:        LongInt;          {session reference number (not }
                                          { used if posting an event)}
      name:             PPCPortRec;       {port name}
      location:         LocationNameRec;  {location name}
      recvrName:        PPCPortRec;       {reserved}
   END;

   TargetIDPtr          = ^TargetID;      {pointer to a target ID record}
   TargetIDHdl          = ^TargetIDPtr;   {handle to a target ID record}
   HighLevelEventMsg =
   RECORD
      HighLevelEventMsgHeaderLength:   Integer;       {reserved}
      version:                         Integer;       {reserved}
      reserved1:                       LongInt;       {reserved}
      theMsgEvent:                     EventRecord;   {event record}
      userRefCon:                      LongInt;       {reference constant}
      postingOptions:                  LongInt;       {reserved}
      msgLength:                       LongInt;       {reserved}
   END;

   HighLevelEventMsgPtr = ^HighLevelEventMsg;
   HighLevelEventMsgHdl = ^HighLevelEventMsgPtr;

   GetSpecificFilterProcPtr = ProcPtr;
   EvQEl =                       {event queue entry}
   RECORD
      qLink:         QElemPtr;   {next queue entry}
      qType:         Integer;    {queue type (ORD(evType))}
      evtQWhat:      Integer;    {event code}
      evtQMessage:   LongInt;    {event message}
      evtQWhen:      LongInt;    {ticks since startup}
      evtQWhere:     Point;      {mouse location}
      evtQModifiers: Integer;    {modifier flags}
   END;

   EvQElPtr = ^EvQEl;

Event Manager Routines

Receiving Events

FUNCTION WaitNextEvent(eventMask: Integer; VAR theEvent: EventRecord; 
sleep: LongInt; mouseRgn: RgnHandle): Boolean;
FUNCTION EventAvail(eventMask: Integer; VAR theEvent: EventRecord)
: Boolean;
FUNCTION GetNextEvent(eventMask: Integer; VAR theEvent: EventRecord)
: Boolean;
FUNCTION AcceptHighLevelEvent
   (VAR sender: TargetID; VAR msgRefcon: LongInt; 
msgBuff: Ptr; VAR msgLen: LongInt): OSErr;
FUNCTION GetSpecificHighLevelEvent
   (aFilter: GetSpecificFilterProcPtr; 
yourDataPtr: UNIV Ptr; VAR err: OSErr) 
: Boolean;
PROCEDURE FlushEvents(whichMask: Integer; stopMask: Integer);
PROCEDURE SystemClick(theEvent: EventRecord; theWindow: WindowPtr);
PROCEDURE SystemTask;
FUNCTION SystemEvent (theEvent: EventRecord): Boolean;
FUNCTION GetOSEvent(mask: Integer; VAR theEvent: EventRecord)
: Boolean;
FUNCTION OSEventAvail(mask: Integer; VAR theEvent: EventRecord)
: Boolean;
PROCEDURE SetEventMask(theMask: Integer);
FUNCTION GetEvQHdr: QHdrPtr;

Sending Events

FUNCTION PostHighLevelEvent(theEvent: EventRecord; receiverID: Ptr; 
msgRefcon: LongInt; msgBuff: Ptr; 
msgLen: LongInt; postingOptions: LongInt) 
: OSErr;
FUNCTION PPostEvent (eventCode: Integer; eventMsg: LongInt;
VAR qEl: EvQElPtr): OSErr;
FUNCTION PostEvent(eventNum: Integer; eventMsg: LongInt): OSErr;

Converting Process Serial Numbers and Port Names

FUNCTION GetProcessSerialNumberFromPortName
   (portName: PPCPortRec; 
VAR PSN: ProcessSerialNumber): OSErr;
FUNCTION GetPortNameFromProcessSerialNumber
   (VAR portName: PPCPortRec; 
PSN: ProcessSerialNumber): OSErr;

Reading the Mouse

PROCEDURE GetMouse(VAR mouseLoc: Point);
FUNCTION Button: Boolean;
FUNCTION StillDown: Boolean;
FUNCTION WaitMouseUp: Boolean;

Reading the Keyboard

PROCEDURE GetKeys(VAR theKeys: KeyMap);
{the KeyTranslate function is also available as the KeyTrans function}
FUNCTION KeyTranslate (transData: Ptr; keycode: Integer; 
                  VAR state: LongInt): LongInt;

Getting Timing Information

FUNCTION TickCount: LongInt;
FUNCTION GetDblTime: LongInt;
FUNCTION GetCaretTime: LongInt;

Application-Defined Routine

Filter Function for Searching the High-Level Event Queue

FUNCTION MyFilter(yourDataPtr: Ptr; 
msgBuff: HighLevelEventMsgPtr; 
sender: TargetID): Boolean;

C Summary

Constants

enum {
      /*event codes*/
      nullEvent               = 0,  /*no other pending events*/
      mouseDown               = 1,  /*mouse button pressed*/
      mouseUp                 = 2,  /*mouse button released*/
      keyDown                 = 3,  /*key pressed*/
      keyUp                   = 4,  /*key released*/
      autoKey                 = 5,  /*key repeatedly held down*/
      updateEvt               = 6,  /*window needs updating*/
      diskEvt                 = 7,  /*disk inserted*/
      activateEvt             = 8,  /*activate/deactivate window*/
      osEvt                   = 15, /*operating-system events */
                                    /* (suspend, resume, mouse-moved)*/
      
      /*event masks*/
      mDownMask               =  2,          /*mouse-down         (bit 1)*/
      mUpMask                 =  4,          /*mouse-up           (bit 2)*/
      keyDownMask             =  8,          /*key-down           (bit 3)*/
      keyUpMask               =  16,         /*key-up             (bit 4)*/
      autoKeyMask             =  32,         /*auto-key           (bit 5)*/
      updateMask              =  64,         /*update             (bit 6)*/
      diskMask                =  128,        /*disk-inserted      (bit 7)*/
      activMask               =  256,        /*activate           (bit 8)*/
      highLevelEventMask      =  1024,       /*high-level         (bit 10)*/
      osMask                  = -32768       /*operating-system   (bit 15)*/
};

enum {
      everyEvent                 = -1,          /*every event*/

      /*event message masks for keyboard events*/
      charCodeMask      = 0x000000FF,        /*use to get character code*/
      keyCodeMask       = 0x0000FF00,        /*use to get key code*/
      adbAddrMask       = 0x00FF0000,        /*ADB address if ADB keyboard*/
      osEvtMessageMask  = 0xFF000000,        /*can use to extract msg code*/



      /*message codes for operating-system events*/
      mouseMovedMessage       = 0xFA,        /*mouse-moved event*/
      suspendResumeMessage    = 0x01,        /*suspend or resume event*/
      /*flags for suspend and resume events*/
      resumeFlag              = 1,           /*resume event*/
      convertClipboardFlag    = 2,           /*Clipboard conversion */
                                             /* required*/

      /*constants corresponding to bits in the modifiers field of event*/
      activeFlag  = 1,        /*bit 0 of low byte--valid only for */
                              /* activate and mouse-moved events*/
      btnState    = 128,      /*bit 7 of low byte is mouse button state*/
      cmdKey      = 256,      /*bit 0 of high byte*/
      shiftKey    = 512,      /*bit 1 of high byte*/
      alphaLock   = 1024,     /*bit 2 of high byte*/
      optionKey   = 2048,     /*bit 3 of high byte*/
      controlKey  = 4096      /*bit 4 of high byte*/
};
enum {
      kHighLevelEvent         = 23, /*event code for high-level events */
                                    /* (includes Apple events)*/
      /*high-level event posting options*/
      receiverIDMask          = 0x0000F000,  /*mask for receiver ID bits*/
      receiverIDisPSN         = 0x00008000,  /*ID is proc serial number*/
      receiverIDisSignature   = 0x00007000,  /*ID is creator signature*/
      receiverIDisSessionID   = 0x00006000,  /*ID is session ref number*/
      receiverIDisTargetID    = 0x00005000,  /*ID is port name & location*/

      systemOptionsMask       = 0x00000F00,
      nReturnReceipt          = 0x00000200,  /*return receipt requested*/
      priorityMask            = 0x000000FF,  /*mask for priority options*/
      nAttnMsg                = 0x00000001,  /*give this message priority*/
      
      /*class and ID values for return receipt*/
      #define HighLevelEventMsgClass 'jaym'
      #define rtrnReceiptMsgID 'rtrn'
      
      /*modifiers values in return receipt*/
      msgWasPartiallyAccepted = 2,
      msgWasFullyAccepted     = 1,
      msgWasNotAccepted       = 0
};

Data Types

   
struct EventRecord { 
      short    what;                   /*event code*/
      long     message;                /*event message*/
      long     when;                   /*ticks since startup*/
      Point    where;                  /*mouse location*/
      short    modifiers;              /*modifier flags*/
   };
typedef struct EventRecord EventRecord;

typedef long KeyMap[4];                /*records state of keyboard*/

struct TargetID {
      long              sessionID;     /*session reference number (not */
                                       /* used if posting an event)*/
      PPCPortRec        name;          /*port name*/
      LocationNameRec   location;      /*location name*/
      PPCPortRec        recvrName;     /*reserved*/
   };

typedef struct TargetID TargetID;
typedef TargetID *TargetIDPtr, **TargetIDHdl;
struct HighLevelEventMsg {
      unsigned short    HighLevelEventMsgHeaderLength;   /*reserved*/
      unsigned short    version;                         /*reserved*/
      unsigned long     reserved1;                       /*reserved*/
      EventRecord       theMsgEvent;                     /*event record*/
      unsigned long     userRefCon;                      /*ref constant*/
      unsigned long     postingOptions;                  /*reserved*/
      unsigned long     msgLength;                       /*reserved*/
   };

typedef struct HighLevelEventMsg HighLevelEventMsg;
typedef HighLevelEventMsg *HighLevelEventMsgPtr, **HighLevelEventMsgHdl;
   struct EvQEl {       /*event queue entry*/
      QElemPtr    qLink;            /*next queue entry*/
      short       qType;            /*queue type (evType)*/
      short       evtQWhat;         /*event code*/
      long        evtQMessage;      /*event message*/
      long        evtQWhen;         /*ticks since startup*/
      Point       evtQWhere;        /*mouse location*/
      short       evtQModifiers;    /*modifier flags*/
   };
   typedef struct EvQEl EvQEl;
   typedef EvQEl *EvQElPtr;
typedef pascal Boolean (*GetSpecificFilterProcPtr) 
(void *yourDataPtr, 
HighLevelEventMsgPtr msgBuff, 
const TargetID *sender);

Event Manager Routines

Receiving Events

pascal Boolean WaitNextEvent(short eventMask, EventRecord *theEvent,
unsigned long sleep, RgnHandle mouseRgn);
pascal Boolean EventAvail(short eventMask, EventRecord *theEvent);
pascal Boolean GetNextEvent(short eventMask, EventRecord *theEvent);
pascal OSErr AcceptHighLevelEvent
   (TargetID *sender, unsigned long *msgRefcon, 
Ptr msgBuff, unsigned long *msgLen); 
pascal Boolean GetSpecificHighLevelEvent
   (GetSpecificFilterProcPtr aFilter, 
void *yourDataPtr, OSErr *err);
pascal void FlushEvents(short whichMask, short stopMask);
pascal void SystemClick(const EventRecord *theEvent,
WindowPtr theWindow);
pascal void SystemTask(void);
pascal Boolean SystemEvent(const EventRecord *theEvent);
pascal Boolean GetOSEvent(short mask, EventRecord *theEvent);
pascal Boolean OSEventAvail(short mask, EventRecord *theEvent);
pascal void SetEventMask(short theMask);
#define GetEvQHdr() ((QHdrPtr) 0x014A)

Sending Events

pascal OSErr PostHighLevelEvent
   (const EventRecord *theEvent, 
unsigned long receiverID, 
unsigned long msgRefcon, Ptr msgBuff, 
unsigned long msgLen, 
unsigned long postingOptions);
pascal OSErr PPostEvent(short eventCode, long eventMsg, EvQElPtr *qEl)
pascal OSErr PostEvent(short eventNum, long eventMsg);

Converting Process Serial Numbers and Port Names

pascal OSErr GetPortNameFromProcessSerialNumber
   (PPCPortPtr portName,
const ProcessSerialNumberPtr pPSN);
pascal OSErr GetProcessSerialNumberFromPortName
   (const PPCPortPtr portName, 
ProcessSerialNumberPtr pPSN);

Reading the Mouse

pascal void GetMouse(Point *mouseLoc);
pascal Boolean Button(void);
pascal Boolean StillDown(void);
pascal Boolean WaitMouseUp(void);

Reading the Keyboard

pascal void GetKeys(KeyMap theKeys);
{the KeyTranslate function is also available as the KeyTrans function}
pascal long KeyTranslate (const void *transData, short keycode, 
long *state);

Getting Timing Information

pascal unsigned long TickCount 
   (void);
#define GetDblTime() (* (unsigned long*) 0x02F0)
#define GetCaretTime() (* (unsigned long*) 0x02F4)

Application-Defined Routine

Filter Function for Searching the High-Level Event Queue

pascal Boolean MyFilter(void *yourDataPtr, 
HighLevelEventMsgPtr msgBuff, 
const TargetID *sender);

Assembly-Language Summary

Data Structures

Event Data Structure
0whatwordevent code
2messagelongevent message
6whenlongticks since startup
10wherelongmouse location
14modifierswordmodifier flags

Target ID Data Structure
0sessionIDlongsession reference number (not used if posting event)
4name68 bytesport name (specified in a PPCPortRec data structure)
72location34 byteslocation name (specified in a LocationNameRec)
106recvrName68 bytesreserved

High-Level Event Message Data Structure
0HighLevelEventMsgHeaderLength
  wordreserved
2versionwordreserved
4reserved1longreserved
8theMsgEvent16 bytes event record
22userRefConlongreference constant
26postingOptionslongreserved
30msgLengthlongreserved

Event Queue Header Data Structure
0qLinklongnext queue entry
4qTypewordqueue type
6evtQWhatwordevent code
8evtQMessagelongevent message
12evtQWhenlongticks since startup
16evtQWherelongmouse location
20evtQModifierswordmodifier flags

Trap Macros

Trap Macros Requiring Routine Selectors

_OSDispatch
SelectorRoutine
$0033AcceptHighLevelEvent
$0034PostHighLevelEvent
$0035GetProcessSerialNumberFromPortName
$0045GetSpecificHighLevelEvent
$0046GetPortNameFromProcessSerialNumber

Trap Macros Requiring Register Setup
Trap macro nameRegisters on entryRegisters on exit
_FlushEventsD0: event mask (low-order word)
stop mask (high-order word)
D0: 0 if all events were removed from the queue, or the event code of the event that stopped the search (low-order word)
_GetOSEventA0:
D0:
address of event record
event mask (low-order word)

D0:

0 if GetOSEvent returns any event other than a null event, or -1 if it returns a null event (low-order byte)
_OSEventAvailA0:
D0:
address of event record
event mask (low-order word)

D0:

0 if OSEventAvail returns any event other than a null event, or -1 if it returns a null event (low-order byte)
_PostEventA0:
D0:
event code (low-order word)
event message (long word)

D0:

result code (low-order word)

Global Variables
CaretTimeThe suggested difference in ticks that should exist between blinks of the caret in editable text.
DoubleTimeThe suggested maximum difference in ticks that should exist between the time of a mouse-up event and a mouse-down event for your application to consider those two mouse events a double click.
EventQueueThe header of the event queue.
KeyRepThreshThe value of the auto-key rate (the amount of time, in ticks, that must elapse before the Event Manager generates a subsequent auto-key event).
KeyThreshThe value of the auto-key threshold (the amount of time, in ticks, that must elapse before the Event Manager generates an auto-key event).
ScrDmpEnableA byte that, if set to 0, disables the Event Manager's processing of Command-Shift-number key combinations with numbers 3 through 9.
SEvtEnbA byte that, if set to 0, causes the SystemEvent function to always return FALSE.
SysEvtMaskThe system event mask of the current application.
TicksA long integer that indicates the current number of ticks since the system last started up.

Result Codes
noErr0No error
procNotFound-600No eligible process with specified process serial number
bufferIsSmall-607Buffer is too small
noOutstandingHLE-608No outstanding high-level event
connectionInvalid-609Connection is invalid
noUserInteractionAllowed-610Cannot interact directly with user
noPortErr-903Invalid port name


Previous Book Contents Book Index Next

© Apple Computer, Inc.
11 JUL 1996