SimpleCxxLib
packageclass GEvent
GEvent
hierarchy looks like this:
The standard paradigm for using GEvent
is illustrated
by the following program, which allows the user to draw lines on the
graphics window:
int main() { GWindow gw; GLine *line; cout << "This program lets the user draw lines by dragging." << endl; while (true) { GMouseEvent e = waitForEvent(MOUSE_EVENT); if (e.getEventType() == MOUSE_PRESSED) { line = new GLine(e.getX(), e.getY(), e.getX(), e.getY()); gw.add(line); } else if (e.getEventType() == MOUSE_DRAGGED) { line->setEndPoint(e.getX(), e.getY()); } } }
Methods | |
Returns the enumerated type constant indicating the class of the event. | |
Returns the system time in milliseconds at which the event occurred. | |
Returns the enumerated type constant corresponding to the specific event type. | |
Returns an integer whose bits indicate what modifiers are in effect. | |
Returns true if the event is valid. | |
Sets the event time field for this event. | |
Sets the modifiers field for this event. | |
Converts the event to a human-readable representation of the event. | |
Functions | |
Checks to see if there are any events of the desired type waiting on the event queue. | |
Waits for a mouse click in any window, discarding any other events. | |
Dismisses the process until an event occurs whose type is covered by the event mask. |
EventClassType getEventClass() const;
Usage:
EventClassType eventClass = e.getEventClass();
EventType getEventType() const;
Usage:
EventType type = e.getEventType();
double getEventTime() const;
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() const;
if (e.getModifiers() & SHIFT_DOWN) ...
Usage:
int modifiers = e.getModifiers();
string toString() const;
Usage:
string str = e.toString();
bool isValid();
true
if the event is valid.
Usage:
if (e.isValid()) ...
void setEventTime(double time);
Usage:
e.setEventTime(time);
void setModifiers(int modifiers);
Usage:
e.setModifiers(modifiers);
void waitForClick();
Usage:
waitForClick();
GEvent waitForEvent(int mask = ANY_EVENT);
e = waitForEvent(MOUSE_EVENT + ACTION_EVENT);The
mask
parameter is optional. If it is missing,
waitForEvent
accepts any event.
As a more sophisticated example, the following code is the canonical event loop for an animated application that needs to respond to mouse, key, and timer events:
GTimer timer(ANIMATION_DELAY_IN_MILLISECONDS); timer.start(); while (true) { GEvent e = waitForEvent(TIMER_EVENT + MOUSE_EVENT + KEY_EVENT); switch (e.getEventClass()) { case TIMER_EVENT: takeAnimationStep(); break; case MOUSE_EVENT: handleMouseEvent(GMouseEvent(e)); break; case KEY_EVENT: handleKeyEvent(GKeyEvent(e)); break; } }
Usage:
GEvent e = waitForEvent(mask);
GEvent getNextEvent(int mask = ANY_EVENT);
waitForEvent
; if not, getNextEvent
returns an invalid event. The mask
parameter is optional.
If it is missing, getNextEvent
accepts any event.
Usage:
GEvent e = getNextEvent(mask);