#include "gevents.h"

class GEvent

This class is the root of the hierarchy for all events. The primary purpose of this general class is as the parameter to the waitForEvent and getNextEvent functions. Code that uses these functions to wait for events of more than one class must typically cast the event to the appropriate subclass, as illustrated in the sample code that accompanies the prototypes for those functions.
Constructor
GEvent() Ensures that an event is properly initialized to a NULL event.
Methods
getEventClass() Returns the enumerated type constant indicating the class of the event.
getEventTime() Returns the system time in milliseconds at which the event occurred.
getModifiers() Returns an integer whose bits indicate what modifiers are in effect.
toString() Converts the event to a human-readable representation of the event.
Operator
if (e) . . . Converts the event to a Boolean value which is true if the event is valid.
Functions
startIntervalTimer(delay)
startIntervalTimer(delay, count) 
Starts an interval timer that fires a timer event repeatedly every delay milliseconds.
stopIntervalTimer() Stops the interval timer.
postEvent(e) Adds the event to the end of the event queue.
waitForEvent(e) Dismisses the process until an event occurs.
waitForClick()
waitForClick(mouseEvent) 
Waits for a mouse click to occur anywhere in the window, discarding any other events.
getNextEvent(e) Checks to see if there are any events waiting on the event queue.

Constructor detail


GEvent();
Ensures that an event is properly initialized to a NULL event.

Usage:

GEvent event;

Method detail


EventClassType getEventClass();
Returns the enumerated type constant indicating the class of the event.

Usage:

EventClassType eventClass = e.getEventClass();

double getEventTime();
Returns the system time in milliseconds at which the event occurred. To ensure portability among systems that represent time in different ways, the StanfordCPPLib packages use type double to represent time, which is always encoded as the number of milliseconds that have elapsed since 00:00:00 UTC on January 1, 1970, which is the conventional zero point for computer-based time systems.

Usage:

double time = e.getEventTime();

int getModifiers();
Returns an integer whose bits indicate what modifiers are in effect. To check whether the shift key is down, for example, one could use the following code:
   if (e.getModifiers() & SHIFT_DOWN) . . .

Usage:

int modifiers = e.getModifiers();

virtual string toString();
Converts the event to a human-readable representation of the event.

Usage:

string str = e.toString();

Operator detail


operator bool();
Converts the event to a Boolean value which is true if the event is valid.

Usage:

if (e) . . .

Function detail


void startIntervalTimer(double delay);
void startIntervalTimer(double delay, int count);
Starts an interval timer that fires a timer event repeatedly every delay milliseconds. The count parameter, if specified, indicates the maximum number of events to fire; if this parameter is missing, the timer continues to fire until it is stopped.

Usage:

startIntervalTimer(delay);
startIntervalTimer(delay, count);

void stopIntervalTimer();
Stops the interval timer.

Usage:

stopIntervalTimer();

void postEvent(GEvent e);
Adds the event to the end of the event queue.

Usage:

postEvent(e);

void waitForEvent(GEvent & e);
void waitForEvent(GWindowEvent & e);
void waitForEvent(GActionEvent & e);
void waitForEvent(GTimerEvent & e);
void waitForEvent(GMouseEvent & e);
void waitForEvent(GKeyEvent & e);
Dismisses the process until an event occurs. When it does, the waitForEvent function returns with the details of the event. The parameter e can be either a general GEvent variable or one of the specific subclasses. In the former case, the function returns when any event occurs. Clients should use this approach if they need to respond to more than one class of event. As an example, the following code is the canonical event loop for an animated application that needs to respond to mouse, key, and timer events:
   startIntervalTimer(ANIMATION_DELAY_IN_MILLISECONDS);
   while (true) {
      GEvent e;
      waitForEvent(e);
      switch (e.getEventClass()) {
       case TIMER_EVENT:
         takeAnimationStep();
         break;
       case MOUSE_EVENT:
         handleMouseEvent(GMouseEvent(e));
         break;
       case KEY_EVENT:
         handleKeyEvent(GKeyEvent(e));
         break;
      }
   }
For applications that are interested only in mouse events, for example, this code can be simplified as follows:
   while (true) {
      GMouseEvent e;
      waitForEvent(e);
      handleMouseEvent(e);
   }

Usage:

waitForEvent(e);

void waitForClick();
void waitForClick(GMouseEvent & mouseEvent);
Waits for a mouse click to occur anywhere in the window, discarding any other events. If the client passes a GMouseEvent as a reference parameter, the function will fill in the details of the click event.

Usage:

waitForClick();
waitForClick(mouseEvent);

bool getNextEvent(GEvent & e);
bool getNextEvent(GWindowEvent & e);
bool getNextEvent(GActionEvent & e);
bool getNextEvent(GTimerEvent & e);
bool getNextEvent(GMouseEvent & e);
bool getNextEvent(GKeyEvent & e);
Checks to see if there are any events waiting on the event queue. If so, getNextEvent fills in the structure of the event with the first event in the queue and returns true. If there are no events, getNextEvent returns false. As with waitForEvent, the parameter e can be either a GEvent variable or one of the specific subclasses. Clients should use this form of the call if they need to support animation in the main thread, as in the following code example:
   while (true) {
      GEvent e;
      if (getNextEvent(e)) {
         switch (e.getEventClass()) {
          case MOUSE_EVENT:
            handleMouseEvent(GMouseEvent(e));
            break;
          case KEY_EVENT:
            handleKeyEvent(GKeyEvent(e));
            break;
         }
      } else {
         takeAnimationStep();
      }
   }

Usage:

if (getNextEvent(e)) . . .