Using the space key

I am trying to make something happen when the space key is pressed.

void ofApp::keyPressed(int key){
    if(choice == 1){
        if(key == 't') choice = 0;

Except instead of ‘t’ I want space. That does not work tho… I also tried the askii 32 with no luck


use the pre-defined OF keys : OF_KEY_DEL, OF_KEY_BACKSPACE etc…

In your case :

if(key == OF_KEY_SPACE)

I also tried that and I get error:
“ofApp.cpp|301|error: ‘OF_KEY_SPACE’ was not declared in this scope”

Strange, that should work, you can also use:

if(key == ' ')
1 Like

Doing this eliminates the error message but still does nothing when the space key is pressed…

I would check first if the key pressed is that one you are looking for, and then apply any kind of logic

void ofApp::keyPressed(int key){
    if(key == OF_KEY_SPACE){
        if(choice == 1) choice = 0;

I’m not sure I follow what you mean…

I have verified that it does what I want if I use a standard letter key.

Hi there,

There’s no OF_KEY_SPACE. But @hahakid suggestion should work. If not, maybe you could post your issue on github.

Openframeworks do not define spacebar key, but you can solve it by its ascii code.
You can also define OF_KEY_SPACE by yourself.
In my case, I define OF_KEY_SPACE as const variable in ofApp.h file.

class ofApp : public ofBaseApp {

const int OF_KEY_SPACE = 32;



Great solution Bro I must be shame of my self because I didn’t think like this.
very easy useful solution.