class GEventwaitForEvent 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. |
GEvent();
NULL
event.
Usage:
GEvent event;
EventClassType getEventClass();
Usage:
EventClassType eventClass = e.getEventClass();
double getEventTime();
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();
if (e.getModifiers() & SHIFT_DOWN) . . .
Usage:
int modifiers = e.getModifiers();
virtual string toString();
Usage:
string str = e.toString();
operator bool();
true if the
event is valid.
Usage:
if (e) . . .
void startIntervalTimer(double delay); void startIntervalTimer(double delay, int count);
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();
Usage:
stopIntervalTimer();
void postEvent(GEvent e);
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);
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);
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);
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)) . . .