Using keyPressed in a class

I’m running into a number of issues trying to instantiate an image from a class with a keypress.

The image class is whiteFlash.

//--------------------------------------------------------------whiteFlash.h

#pragma once
#include "ofMain.h"

class whiteFlash {
public:


	void setup() {
		ofAddListener(ofEvents.keyPressed, this, &whiteFlash::_keyPressed);
	}
	void update();
	void draw();
	void _keyPressed(int _key);

	whiteFlash();

	ofImage phone;

	bool hit = false;

};
//--------------------------------------------------------------whiteFlash.cpp

#include "whiteFlash.h"

whiteFlash::whiteFlash() {

}

void whiteFlash::setup() {
	hit = false;
	phone.loadImage("LL.jpg");
}

void whiteFlash::update() {
	
}

void whiteFlash::draw() {
	if (hit == true) {
		phone.draw(100, 100);
	}
}

void whiteFlash::_keyPressed(int _key) {
	if (_key == 'z') {
		hit = true;
		ofSleepMillis(4000);
		hit = false;
	}
}
//--------------------------------------------------------------ofApp.h

#pragma once

#include "ofMain.h"
#include "whiteFlash.h"

class ofApp : public ofBaseApp {

public:
	void setup();
	void update();
	void draw();

	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y);
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void mouseEntered(int x, int y);
	void mouseExited(int x, int y);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);

	bool _hit = false;

	vector<float> myFloats;

	const int MAX_SIZE = 100;

	ofVec2f gravity;

	whiteFlash myFlash;
};
//--------------------------------------------------------------ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup() {
	ofBackground(0);
	myFlash.setup();
}

//--------------------------------------------------------------
void ofApp::update() {
	myFlash.update();
}

//--------------------------------------------------------------
void ofApp::draw() {
	myFlash.draw();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
	myFlash._keyPressed(int _key);
}

I’m trying to load the image by setting a bool to true in when ‘z’ is pressed but I’ve run into two issues that have been making this a bit of a challenge.

errors: http://imgur.com/a/e5FkW

First: I’m trying to use ofAddListener() to use the keyPress function in the whiteFlash class. I can’t tell what I have to change in order for this to work properly but from what I understand, I can’t use key events in a class if I don’t use ofAddListener. If I’m using this wrong or just don’t get how it works, let me know.

Second: in ofApp.cpp, when I call “myFlash.keyPressed();” in “ofApp::keyPressed”, I get the error: “function does not take zero arguements”. However, when I write “myFlash.keyPressed(int _key)” I get 3 errors saying: “type name is not allowed”, "expected a ‘)’ ", and “type ‘int’ unexpected”.

Any advice is much appreciated

Thanks!

Hi. Your whiteFlash class should look like:

//whiteFlash.h
#pragma once
#include "ofMain.h"

class whiteFlash {
public:


void setup();
void update();
void draw();
void _keyPressed(int _key);

whiteFlash();

ofImage phone;

bool hit = false;

};

.cpp

#include "whiteFlash.h"
whiteFlash::whiteFlash() {
    
}

void whiteFlash::setup() {
hit = false;
phone.load("LL.jpg");

}

void whiteFlash::update() {

}

void whiteFlash::draw() {
if (hit == true) {
    phone.draw(100, 100);
}
}

void whiteFlash::_keyPressed(int _key) {
if (_key == 'z') {
    hit = true;
    //ofSleepMillis(4000);
    //hit = false;
}
}

ofApp.cpp

void ofApp::keyPressed(int key){
myFlash._keyPressed(key);
}

It should be with brackets:
ofAddListener(ofEvents().keyPressed, this, &whiteFlash::_keyPressed);

You can get core events in your own class. More details here:

Thanks, this worked. I noticed that using

if (_key == ‘z’) {
hit = true;
ofSleepMillis(4000);
hit = false;
}

Was preventing the image to appear.
I commented the the bottom two lines out like suggested and now the bool is set to true and the image stays on screen indefinitely. What I would like to do is set the “hit” bool to false after 4 seconds, do you have any suggestions on how to do this?
Thanks

You can use a timer. In your method _keyPressed set a variable float startTime defined in h:

hit = true;
startTime = ofGetElapsedTimeMillis();

and check it in update()

if (ofGetElapsedTimeMillis() - startTime) > 4000 {
hit = false;
}