How to get core events/functions into another class

I’m trying to get the setup, key events, update, draw, etc events to work in a class that isn’t in testApp, but I can’t figure out what to extend or if I need eventlisteners to add to my class. Is there a simple way to get these event calls just like testApp does? I tried extending ofBaseApp, but that didn’t work.

Hey Seth, you don’t need to extend anything, just add the functions as event listeners:

ofAddListener(ofEvents.mousePressed, this, &MyClass::_mousePressed);  
ofAddListener(ofEvents.mouseMoved, this, &MyClass::_mouseMoved);  
ofAddListener(ofEvents.mouseDragged, this, &MyClass::_mouseDragged);  
ofAddListener(ofEvents.mouseReleased, this, &MyClass::_mouseReleased);  
ofAddListener(ofEvents.keyPressed, this, &MyClass::_keyPressed);  
ofAddListener(ofEvents.keyReleased, this, &MyClass::_keyReleased);  
ofAddListener(ofEvents.setup, this, &MyClass::_setup);  
ofAddListener(ofEvents.update, this, &MyClass::_update);  
ofAddListener(ofEvents.draw, this, &MyClass::_draw);  
ofAddListener(ofEvents.exit, this, &MyClass::_exit);  

It’s not hard to do, but I have a class (ofxMSAInteractiveObject) which wraps it up, and you can just extend that class for the functionality…-Object.cpp

Thanks memo. Much appreciated! :slight_smile:

i’ve been trying to do:

	ofAddListener(ofEvents.keyPressed, this, &scrPointCloud::keyPressed);  

but i get:

error: no matching function for call to ‘ofAddListener(ofEvent&, scrPointCloud* const, void (scrPointCloud::*)(int))’

my function local function looks like this:


void scrPointCloud::keyPressed(int key)  
	cout << "" << key << endl;  


	void			keyPressed(int key);  

any hints?

your key event listener needs to look something like this,

void keyPressed ( ofKeyEventArgs& eventArgs )  

you can access the key pressed by,



This updated in .9
In your class you should add an event that is listening for the event you want like so:

class MyClass { void myFunction(ofKeyEventArgs &e); }

// a full list possible events looks like this
void mousePressed(ofMouseEventArgs &e);
void mouseReleased(ofMouseEventArgs &e);
void mouseMoved(ofMouseEventArgs &e);
void mouseDragged(ofMouseEventArgs &e);
void keyPressed(ofKeyEventArgs &e);
void keyReleased(ofKeyEventArgs &e);
void windowResized(ofResizeEventArgs &e);

If you instatiate your class in ofApp,

MyClass *my_class; my_class = new MyClass;

you can add all the events there if you want like so:
then enable events like:

ofAddListener(ofEvents().mousePressed, my_class, &MyClass::mousePressed);
ofAddListener(ofEvents().mouseMoved, my_class, &MyClass::mouseMoved);
ofAddListener(ofEvents().mouseDragged, my_class, &MyClass::mouseDragged);
ofAddListener(ofEvents().mouseReleased, my_class, &MyClass:mouseReleased);
ofAddListener(ofEvents().keyPressed, my_class, &MyClass::keyPressed);
ofAddListener(ofEvents().keyReleased, my_class, &MyClass::keyReleased);
ofAddListener(ofEvents().windowResized, my_class, &MyClass::windowResized);