StanfordCPPLib
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
GEvent Class Reference

A GEvent represents a user action that has occurred on a graphical interactor. More...

#include "gevent.h"

Public Types

enum  KeyCode {
  BACKSPACE_KEY = 8, TAB_KEY = 9, ENTER_KEY = 10, CLEAR_KEY = 12, RETURN_KEY = 13, SHIFT_KEY = Qt::Key_Shift, CTRL_KEY = Qt::Key_Control, ALT_KEY = Qt::Key_Alt, PAUSE_KEY = 19, CAPS_LOCK_KEY = 20, ESCAPE_KEY = 27, PAGE_UP_KEY = Qt::Key_PageUp, PAGE_DOWN_KEY = Qt::Key_PageDown, END_KEY = Qt::Key_End, HOME_KEY = Qt::Key_Home, LEFT_ARROW_KEY = Qt::Key_Left, UP_ARROW_KEY = Qt::Key_Up, RIGHT_ARROW_KEY = Qt::Key_Right, DOWN_ARROW_KEY = Qt::Key_Down, F1_KEY = Qt::Key_F1,
  F2_KEY = Qt::Key_F2, F3_KEY = Qt::Key_F3, F4_KEY = Qt::Key_F4, F5_KEY = Qt::Key_F5, F6_KEY = Qt::Key_F6, F7_KEY = Qt::Key_F7, F8_KEY = Qt::Key_F8, F9_KEY = Qt::Key_F9, F10_KEY = Qt::Key_F10, F11_KEY = Qt::Key_F11, F12_KEY = Qt::Key_F12, DELETE_KEY = 127, NUM_LOCK_KEY = Qt::Key_NumLock, SCROLL_LOCK_KEY = Qt::Key_ScrollLock, PRINT_SCREEN_KEY = Qt::Key_Print, INSERT_KEY = Qt::Key_Insert, HELP_KEY = Qt::Key_Help, META_KEY = Qt::Key_Meta, WINDOWS_KEY = Qt::Key_Super_L, MENU_KEY = Qt::Key_Menu
}
 

Public Member Functions

 GEvent(EventClass eventClass=NULL_EVENT, EventType eventType=NULL_TYPE, string eventName="", GObservable *source=nullptr)
 Creates a new event of the given type. More...
 
~GEvent()
 Frees memory allocated internally by the event. More...
 
string getActionCommand() const
 Returns the action command associated with the event. More...
 
int getButton() const
 Returns which mouse button was clicked, if this is a mouse event. More...
 
EventClass getClass() const
 Returns this event's class (major type such as MOUSE_EVENT). More...
 
int getColumn() const
 Returns the column that was interacted with, if this is a table event. More...
 
EventClass getEventClass() const
 Returns this event's class (major type such as MOUSE_EVENT). More...
 
EventType getEventType() const
 Returns the event's type (minor type such as MOUSE_PRESSED). More...
 
GInteractorgetInteractor() const
 Returns the source interactor that generated this event. More...
 
QEvent * getInternalEvent() const
 Returns the Qt event being wrapped by this event, if any. More...
 
char getKeyChar() const
 Returns the key character that was typed, if this is a key event. More...
 
int getKeyCode() const
 Returns the integer key code that was typed, if this is a key event. More...
 
GPoint getLocation() const
 Returns an (x, y) point representing the mouse position within the interactor when this event occurred. More...
 
int getModifiers() const
 Returns the modifiers active during this event. More...
 
string getName() const
 Returns this event's name such as "click" or "keydown" or "actionperformed". More...
 
string getRequestURL() const
 Returns this event's request URL, if this is a server URL event. More...
 
int getRow() const
 Returns the row that was interacted with, if this is a table event. More...
 
GObservablegetSource() const
 Returns the source object that generated this event. More...
 
long getTime() const
 Returns this event's timestamp, as a number of milliseconds elapsed since the epoch of 1970/01/01 12:00am. More...
 
EventType getType() const
 Returns the event's type (major type such as MOUSE_EVENT). More...
 
double getX() const
 Returns the x-coordinate of the mouse position within the interactor when this event occurred. More...
 
double getY() const
 Returns the y-coordinate of the mouse position within the interactor when this event occurred. More...
 
void ignore()
 Instructs the GUI system to ignore or cancel this event. More...
 
bool isAltKeyDown() const
 Returns true if the Alt key was held down during this event. More...
 
bool isCtrlKeyDown() const
 Returns true if the Ctrl key was held down during this event. More...
 
bool isCtrlOrCommandKeyDown() const
 Returns true if the Ctrl key, or the Command key (Mac), was held down during this event. More...
 
bool isDoubleClick() const
 Returns true if the user pressed the mouse button multiple times. More...
 
bool isLeftClick() const
 Returns true if the user pressed the left mouse button. More...
 
bool isMetaKeyDown() const
 Returns true if the Meta/Command key was held down during this event. More...
 
bool isMiddleClick() const
 Returns true if the user pressed the middle mouse button. More...
 
bool isRightClick() const
 Returns true if the user pressed the right mouse button. More...
 
bool isShiftKeyDown() const
 Returns true if the Shift key was held down during this event. More...
 
string toString() const
 Returns a text representation of the event for debugging. More...
 

Static Public Member Functions

static string keyCodeToString(int keyCode)
 Converts a key code such as 67 into a string such as "A". More...
 
static string typeToString(EventType eventType)
 Converts an event type such as MOUSE_EVENT to a string such as "MOUSE_EVENT". More...
 

Static Public Attributes

static GEventListener EMPTY_EVENT_LISTENER = __emptyEventListener
 An empty event handler that can be passed that does nothing. More...
 
static GEventListener LOG_EVENT = __logEventListener
 An event listener that just prints the event that occurred. More...
 

Detailed Description

A GEvent represents a user action that has occurred on a graphical interactor.

Older versions of this library used an event-polling model where the client was encouraged to write a while (true) loop and call waitForEvent(...) to get each event and process it. The current design instead prefers that you attach event listener functions to be called when events occur. These listener functions can accept an optional GEvent as a parameter. The GEvent object will contain information about the event that occurred.

Older versions of this library had an inheritance hierarchy for various event types, such as GMouseEvent, GKeyEvent, etc. The current design has a single type GEvent that is a union of all data needed by any kind of event. The previous subclass names such as GMouseEvent are retained for backward compatibility, but they are now just aliases for the type GEvent.

Member Enumeration Documentation

◆ KeyCode

enum KeyCode
Enumerator
BACKSPACE_KEY 
TAB_KEY 
ENTER_KEY 
CLEAR_KEY 
RETURN_KEY 
SHIFT_KEY 
CTRL_KEY 
ALT_KEY 
PAUSE_KEY 
CAPS_LOCK_KEY 
ESCAPE_KEY 
PAGE_UP_KEY 
PAGE_DOWN_KEY 
END_KEY 
HOME_KEY 
LEFT_ARROW_KEY 
UP_ARROW_KEY 
RIGHT_ARROW_KEY 
DOWN_ARROW_KEY 
F1_KEY 
F2_KEY 
F3_KEY 
F4_KEY 
F5_KEY 
F6_KEY 
F7_KEY 
F8_KEY 
F9_KEY 
F10_KEY 
F11_KEY 
F12_KEY 
DELETE_KEY 
NUM_LOCK_KEY 
SCROLL_LOCK_KEY 
PRINT_SCREEN_KEY 
INSERT_KEY 
HELP_KEY 
META_KEY 
WINDOWS_KEY 
MENU_KEY 

Constructor & Destructor Documentation

◆ GEvent()

GEvent ( EventClass  eventClass = NULL_EVENT,
EventType  eventType = NULL_TYPE,
string   eventName = "",
GObservable source = nullptr 
)

Creates a new event of the given type.

◆ ~GEvent()

~GEvent ( )
virtual

Frees memory allocated internally by the event.

Member Function Documentation

◆ getActionCommand()

string getActionCommand ( ) const
virtual

Returns the action command associated with the event.

For some interactors such as buttons, this will be the text of the interactor.

◆ getButton()

int getButton ( ) const
virtual

Returns which mouse button was clicked, if this is a mouse event.

If this is not a mouse event, returns 0.

◆ getClass()

EventClass getClass ( ) const
virtual

Returns this event's class (major type such as MOUSE_EVENT).

Equivalent to getEventClass.

◆ getColumn()

int getColumn ( ) const
virtual

Returns the column that was interacted with, if this is a table event.

If this is not a table event, returns 0.

◆ getEventClass()

EventClass getEventClass ( ) const
virtual

Returns this event's class (major type such as MOUSE_EVENT).

Equivalent to getClass.

◆ getEventType()

EventType getEventType ( ) const
virtual

Returns the event's type (minor type such as MOUSE_PRESSED).

Equivalent to getType.

◆ getInteractor()

GInteractor * getInteractor ( ) const
virtual

Returns the source interactor that generated this event.

◆ getInternalEvent()

QEvent * getInternalEvent ( ) const
virtual

Returns the Qt event being wrapped by this event, if any.

If this event does not wrap a Qt event, returns nullptr.

◆ getKeyChar()

char getKeyChar ( ) const
virtual

Returns the key character that was typed, if this is a key event.

If this is not a key event, returns '\0'.

◆ getKeyCode()

int getKeyCode ( ) const
virtual

Returns the integer key code that was typed, if this is a key event.

See the KeyCode enumeration for helpful constants for comparing key values. If this is not a key event, returns 0.

◆ getLocation()

GPoint getLocation ( ) const
virtual

Returns an (x, y) point representing the mouse position within the interactor when this event occurred.

If this is not a mouse event, returns (0, 0).

◆ getModifiers()

int getModifiers ( ) const
virtual

Returns the modifiers active during this event.

See the Modifiers enumeration for more information.

◆ getName()

string getName ( ) const
virtual

Returns this event's name such as "click" or "keydown" or "actionperformed".

◆ getRequestURL()

string getRequestURL ( ) const
virtual

Returns this event's request URL, if this is a server URL event.

If this is not a server URL event, returns an empty string.

◆ getRow()

int getRow ( ) const
virtual

Returns the row that was interacted with, if this is a table event.

If this is not a table event, returns 0.

◆ getSource()

GObservable * getSource ( ) const
virtual

Returns the source object that generated this event.

◆ getTime()

long getTime ( ) const
virtual

Returns this event's timestamp, as a number of milliseconds elapsed since the epoch of 1970/01/01 12:00am.

◆ getType()

EventType getType ( ) const
virtual

Returns the event's type (major type such as MOUSE_EVENT).

Equivalent to getEventType.

◆ getX()

double getX ( ) const
virtual

Returns the x-coordinate of the mouse position within the interactor when this event occurred.

If this is not a mouse event, returns 0.

◆ getY()

double getY ( ) const
virtual

Returns the y-coordinate of the mouse position within the interactor when this event occurred.

If this is not a mouse event, returns 0.

◆ ignore()

void ignore ( )
virtual

Instructs the GUI system to ignore or cancel this event.

For example, if you listen to window-closing events and ignore them, the window will stay open.

◆ isAltKeyDown()

bool isAltKeyDown ( ) const
virtual

Returns true if the Alt key was held down during this event.

If this is not a mouse or key event, returns false.

◆ isCtrlKeyDown()

bool isCtrlKeyDown ( ) const
virtual

Returns true if the Ctrl key was held down during this event.

If this is not a mouse or key event, returns false.

◆ isCtrlOrCommandKeyDown()

bool isCtrlOrCommandKeyDown ( ) const
virtual

Returns true if the Ctrl key, or the Command key (Mac), was held down during this event.

If this is not a mouse or key event, returns false.

◆ isDoubleClick()

bool isDoubleClick ( ) const
virtual

Returns true if the user pressed the mouse button multiple times.

If this is not a mouse event, returns false.

◆ isLeftClick()

bool isLeftClick ( ) const
virtual

Returns true if the user pressed the left mouse button.

If this is not a mouse event, returns false.

◆ isMetaKeyDown()

bool isMetaKeyDown ( ) const
virtual

Returns true if the Meta/Command key was held down during this event.

If this is not a mouse or key event, returns false.

◆ isMiddleClick()

bool isMiddleClick ( ) const
virtual

Returns true if the user pressed the middle mouse button.

(Note that not every mouse has a simple delineation of "left, right, and middle" buttons; this was implemented on a standard 3-button mouse with scroll wheel.) If this is not a mouse event, returns false.

◆ isRightClick()

bool isRightClick ( ) const
virtual

Returns true if the user pressed the right mouse button.

If this is not a mouse event, returns false.

◆ isShiftKeyDown()

bool isShiftKeyDown ( ) const
virtual

Returns true if the Shift key was held down during this event.

If this is not a mouse or key event, returns false.

◆ keyCodeToString()

string keyCodeToString ( int  keyCode)
static

Converts a key code such as 67 into a string such as "A".

Works for special keys such as "Enter" and "Tab".

◆ toString()

string toString ( ) const
virtual

Returns a text representation of the event for debugging.

◆ typeToString()

string typeToString ( EventType  eventType)
static

Converts an event type such as MOUSE_EVENT to a string such as "MOUSE_EVENT".

Friends And Related Function Documentation

◆ GInteractor

friend class GInteractor
friend

◆ GObservable

friend class GObservable
friend

Member Data Documentation

◆ EMPTY_EVENT_LISTENER

GEventListener EMPTY_EVENT_LISTENER = __emptyEventListener
static

An empty event handler that can be passed that does nothing.

◆ LOG_EVENT

GEventListener LOG_EVENT = __logEventListener
static

An event listener that just prints the event that occurred.

This listener is useful for debugging.